Python iteration on subprocess.Popen () stdout / stderr

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 #example of use args = ["./foo"] shell = Shell() for line in shell(args): #do something with line print 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?

+7
source share
1 answer

Check out How to clear Python print output? .

You need to start the python subprocess with the -u option:

-u Force stdin, stdout and stderr are not fully loaded. On a system where this is important, also put stdin, stdout and stderr in binary mode. Note: internal buffering in xreadlines (), readlines (), and file object iterators ("for a line in sys.stdin") are not affected by this option. To get around this, you will want to use "sys.stdin.readline ()" inside the "1:" loop.

Or, if you have control over the python script subprocess, you can use sys.stdout.flush () to clear the output every time you print.

 import sys sys.stdout.flush() 
+2
source

All Articles