How do I get ‘real-time’ information back from a subprocess.Popen in python (2.5)

Update with code that appears not to work (on windows anyway) class ThreadWorker(threading.Thread): def __init__(self, callable, *args, **kwargs): super(ThreadWorker, self).__init__() self.callable = callable self.args = args self.kwargs = kwargs self.setDaemon(True) def run(self): try: self.callable(*self.args, **self.kwargs) except wx.PyDeadObjectError: pass except Exception, e: print e if __name__ == “__main__”: import os from subprocess import Popen, PIPE def … Read more

Python subprocess.Popen() error (No such file or directory)

You should pass the arguments as a list (recommended): subprocess.Popen([“wc”, “-l”, “sorted_list.dat”], stdout=subprocess.PIPE) Otherwise, you need to pass shell=True if you want to use the whole “wc -l sorted_list.dat” string as a command (not recommended, can be a security hazard). subprocess.Popen(“wc -l sorted_list.dat”, shell=True, stdout=subprocess.PIPE) Read more about shell=True security issues here.

Intercepting stdout of a subprocess while it is running

As Charles already mentioned, the problem is buffering. I ran in to a similar problem when writing some modules for SNMPd, and solved it by replacing stdout with an auto-flushing version. I used the following code, inspired by some posts on ActiveState: class FlushFile(object): “””Write-only flushing wrapper for file-type objects.””” def __init__(self, f): self.f = … Read more

Running a process in pythonw with Popen without a console

From here: import subprocess def launchWithoutConsole(command, args): “””Launches ‘command’ windowless and waits until finished””” startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW return subprocess.Popen([command] + args, startupinfo=startupinfo).wait() if __name__ == “__main__”: # test with “pythonw.exe” launchWithoutConsole(“d:\\bin\\gzip.exe”, [“-d”, “myfile.gz”]) Note that sometimes suppressing the console makes subprocess calls fail with “Error 6: invalid handle”. A quick fix is … Read more

How can I specify working directory for popen

subprocess.Popen takes a cwd argument to set the Current Working Directory; you’ll also want to escape your backslashes (‘d:\\test\\local’), or use r’d:\test\local’ so that the backslashes aren’t interpreted as escape sequences by Python. The way you have it written, the \t part will be translated to a tab. So, your new line should look like: … Read more

Popen waiting for child process even when the immediate child has terminated

You could provide start_new_session analog for the C subprocess: #!/usr/bin/env python import os import sys import platform from subprocess import Popen, PIPE # set system/version dependent “start_new_session” analogs kwargs = {} if platform.system() == ‘Windows’: # from msdn [1] CREATE_NEW_PROCESS_GROUP = 0x00000200 # note: could get it from subprocess DETACHED_PROCESS = 0x00000008 # 0x8 | … Read more

Subprocess.Popen: cloning stdout and stderr both to terminal and variables

To capture and display at the same time both stdout and stderr from a child process line by line in a single thread, you could use asynchronous I/O: #!/usr/bin/env python3 import asyncio import os import sys from asyncio.subprocess import PIPE @asyncio.coroutine def read_stream_and_display(stream, display): “””Read from stream line by line until EOF, display, and capture … Read more