Edit: Oops! I accidentally posted code that didn’t match my question. I started writing this post before I was done experimenting, so I posted code from an intermediate point in my testing process. I’ve amended this question to reflect what I really meant to post by changing the code slightly.
The following Python code works as expected with one problem: unless I If verbose == False, the file produced by the Perl script is not created. Why do I have to call output.stdout.read() for the underlying Perl script to successfully create a file?
cmdStringList = ["perl","script.pl","arg1",...]
output = subprocess.Popen(cmdStringList,stdout=subprocess.PIPE)
if verbose:
print output.stdout.read()
I didn’t even realize anything was wrong until I tried to execute my Python script with verbose=False in a production environment. I did some google-fu to try to understand the behavior of Popen and subprocess, but I haven’t come up with a reason for this behavior. Any help would be greatly appreciated.
You actually have the following code, right?
It’s quite likely that the problem is that the child is blocked trying to write to STDOUT. Until you make some space in the pipe by reading from it, the child won’t be able to finish writing to STDOUT and move on to where it creates the file to which you refer.
If you want to prevent the child from blocking without reading, redirect its stdout to
nul(Windows) or/dev/null(elsewhere). This should work everywhere but Windows:(Pardon any syntax errors. I don’t know Python at all.)