Trying to redirect a subprocess\' output to a file.
server.py:
while 1:
print \"Count \" + str(count)
sys.stdout.flush()
count = count +
Output redirection with ">" is a feature of shells - by default, subprocess.Popen
doesn't instantiate one. This should work:
server = subprocess.Popen(args, shell=True)
Altenatively, you can use the stdout
parameter with a file object:
with open('temp.txt', 'w') as output:
server = subprocess.Popen('./server.py', stdout=output)
server.communicate()
As explained in the documentation:
stdin, stdout and stderr specify the executed program’s standard input, standard output and standard error file handles, respectively. Valid values are PIPE, an existing file descriptor (a positive integer), an existing file object, and None.