They live under /bin, just do a which ls.Īnd thanks for the useful post and comments :) For example: wgproc = subprocess.Popen(, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) But when you submit the command with shell=False, you must issue the command as a list of strings, with the command name in the first element of the list, the first argument in the next list element, etc. When you submit a command with shell=True, you submit the whole command as one string. Using shell=False (the default) will save you from having to start a new shell process (which is an expensive operation). You can use shell=True, but you don't need to. ls and rmdir are not programs, they are internal commands within the shell program. I'm surprised you had any success running ls and rmdir without shell=True. You got "'STDOUT' is not defined" because you must either: from subprocess import STDOUT or you must refer to STDOUT as subprocess.STDOUT.
P = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE) But, the following seems to work fine with wget but with wget it seems to make errors whatever you do. This one is not good enough either: p = Popen(cmd, stdout=PIPE, stderr=PIPE) Python says: NameError: name 'STDOUT' is not defined There is error here: p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) I just took my example from the Replacing os.popen* section in the docs. This looks like an interesting alternative. (Disclaimer: I wrote it.) They let you do some cool things. That way you wait for the output until the process has actually completed.Īlso, take a look at the Process objects in the cliutils package. Here's a cleaner way: from subprocess import Popen, PIPE
Add your real code here.įor line in lines : print ( line ) if not readable : # This OutStream is finished.įds. read_lines () # Example: Just print every line. select ( fds, , ) break except InterruptedError : continue # Handle all file descriptors that are ready.įor f in rlist : lines, readable = f. While True : try : rlist, _, _ = select. close ( err_w ) fds = while fds : # Call select(), anticipating interruption by signals. close ( out_w ) # if we do not write to process, close these. Popen (, stdout = out_w, stderr = err_w ) os. That it has a method fileno() that returns the file descriptor as an It can also do the sameįor descriptors that should be written to, details can be found inĪll that is required from an object passed to Python’s select() is
List of those that are ready to be read from. You pass a list of file descriptors and it returns a Treat stdout and stderr differently? Here, the select() system callĬomes into play. More than one process? Or, to make the example simple, you want to This is all very nice, but what if you want to capture the output of You could improve the read_lines() method to add theĬorrect newline characters where needed if that is important. When readable is false, though, the last line did not have a The final newlines are stripped, so re-add them if if not readable : breakĮvery time read_lines() returns, we may get zero, one, or many lines So the main loop looks like this now:į = OutStream ( out_r ) while True : lines, readable = f. The second return value indicates if we should keep reading (true), or Note that we call decode() on all finished lines in order to get Somehow carriage returns (\r) appear in the output when using pseudo Lines = finished_lines = lines readable = False finished_lines = return finished_lines, readable _buffer = b"" if len ( lines ) = 1 and not lines : # We did not have buffer left, so no output at all.
_buffer = lines finished_lines = lines readable = True else : self. _fileno, 1000 ) except OSError as e : if e. _buffer = b"" def read_lines ( self ): try : output = os. Let’s make a class for that, which will come in handyĬlass OutStream : def _init_ ( self, fileno ): self. For this, you need to buffer unfinished lines and thus You probably prefer having it in theįorm of lines. The output is still in the form of random bytes from somewhere in the Therefore, you want to break out of the loop when you get Simulating os.read()’s behaviour in case of reaching the end of theįile ( EOF). In this case, output is set to the empty string, Needed because an EIO error is raised when you can no longer read from If the application did not do any custom buffering, you should getĮach line as soon as it is finished. Note that os.read() returns bytes, but we cannot decode EIO : raise output = b"" if not output : break. read ( out_r, 1000 ) except OSError as e : if e. close ( out_w ) # if we do not write to process, close this.