There are many similar posts, but I did not find the answer.
On Gnu / Linux, with the Python module and subprocess , I use the following code to iterate over the stdout / sdterr command running with the subprocess:
class Shell: """ run a command and iterate over the stdout/stderr lines """ def __init__(self): pass def __call__(self,args,cwd='./'): p = subprocess.Popen(args, cwd=cwd, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, ) while True: line = p.stdout.readline() self.code = p.poll() if line == '': if self.code != None: break else: continue yield line
This works fine unless the command is executed by Python , for example, `args = ['python', 'foo.py'], in which case the output is not cleared, and is printed only when the command is completed.
Is there a solution?
user744629
source share