I'm currently trying to redirect the standard output of the subprocess.Popen object to an opened file, so I followed the instructions found in many websites such as this.
However, the Popen.stdout is not correctly assigned for some reason
My code went as follows:
def foo(file):
print(file) # printA
proc = subprocess.Popen('command, stdout=file) # i've tried with both shell=True/False
print(proc) # printB
print(proc.stdout) # printC
return proc
def main():
file = open('path', 'w')
p = foo(file)
print(p.stdout) # printD
The result is as of follows
printA: <_io.TextIOWrapper name='path' mode='w' encoding='UTF-8'>
printB: <subprocess.Popen object at 0x161966d0>
printC: None
printD: None
from what I read in the python doc here, the default for stdout is None if no PIPE is assigned to it. But since this here shows that apparently we can pass an opened file as the argument to stdout, I don't understand why my code doesn't work.
Please help.