Python Subprocess Finishes, but Output File Not Available
Question: My python code is using a subprocess to call "ifconfig" through the shell and
uses ">" to write the output to a text file. When the subprocess finishes, and
returns success, I read the output file. I do this at fixed intervals to
monitor the network status, but occasionally I'm unable to open the output
file. I was just reading that Popen has optional arguments for stdout and
stderr, which may be safer/better supported, but I'm curious to why my current
version fails. My code is below. There are a few objects and macros from my
library included without explanation, but I think the code is still clear
enough for this question.

Why does opening the output file occasionally fail? Is it possible the file is
not ready when the subprocess returns? What would be a way to guarantee it's
ready to open?

    
    
           # Build command line expression.
            expr = 'ifconfig' + ' >' + outputFile + ' 2>&1'
    
            try:
                # Execute command line expression.
                p = subprocess.Popen(expr, shell=True)
            except:
                Error("Unable to open subprocess.")
    
            if(p is None):
                Error("Unable to create subprocess.")
    
            # Wait until command line expression has been executed.
            wait = Wait.Wait(Constants.MIN_TIME_TO_QUERY_NETWORK_INFO, Constants.MAX_TIME_TO_QUERY_NETWORK_INFO)
    
            #Execute command then wait for timeout.    
            if (wait.StopUntilCondition(operator.ne, (p.poll,), None, True)):
                p.kill()
                Error("Get subnet mask subprocess timed out.")
    
            if(not p.poll() == 0):
                Error("Failed to get network information from operating system.")
    
            Warning("About to read output file from get subnet mask...")
    
            # Read temporary output file.
            f = open(outputFile, "r")
            networkInfo = f.read()
            f.close()
    


Answer: To avoid the corrupted/missing output, you should call `p.wait()` before
trying to read the file. You don't need to use a file in this case:

    
    
    from subprocess import check_output, STDOUT
    
    network_info = check_output('ifconfig', stderr=STDOUT)
    

If you want to interrupt `ifconfig` before it is done _and_ read its output;
see [Stop reading process output in Python without
hang?](http://stackoverflow.com/q/4417962/4279)

