I have seen several questions around this topic already but none has worked for me. What I need is to get hold of the complete stdout and stderr of a subprocess.Popen([...],stdout=subprocess.PIPE)
and write it all at once to a file, for example:
import tempfile
import subprocess
stattmpfile = tempfile.NamedTemporaryFile(suffix=".log",prefix="status",delete=False)
proc = subprocess.Popen([mycommand, myparams], stdout=subprocess.PIPE)
while proc.poll() is None:
output = proc.stdout.readline()
statusfile.write(output)
output = proc.communicate()[0]
statusfile.write(output)
statusfile.close()
In this example I only get the first line of the standard output and nothing else.
To save both stdout and stderr of a subprocess to a file:
import subprocess
with open('filename', 'wb', 0) as file:
subprocess.check_call(cmd, stdout=file, stderr=subprocess.STDOUT)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.