I am running a subprocess using 'Popen'. I need to block till this subprocess finishes and then read its output.
p = Popen(command, stdin=PIPE, stdout=PIPE, stderr=PIPE, encoding="utf-8")
p.communicate():
output = p.stdout.readline()
print(output)
I get an error that
ValueError: I/O operation on closed file.
How can I read the output after the subprocess finishes, I do not want to use poll() though as the subprocess takes time and I would need to wait for its completion anyway.
p.communicate()returns the output.subprocess.run()or the legacysubprocess.check_output()? You should avoidPopenif you can precisely because it's tricky to get right.p.communicate()returnsbound method Popen.communicate of <subprocess.Popen object at 0x105e84e10, not the output of the subprocess codeoutput, error = p.communicate()should work, that output looks like you are just printingp.communicate(without the parentheses).