Error Explanation
It looks like this error is occurring because upon exiting, the program tries to destroy res
, which involves calling the res.close()
method. But somehow invoking os.wait()
has already closed the object. So it's trying to close res
twice, resulting in the error. If the call to os.wait()
is removed, the error no longer occurs.
import os
try:
res = os.popen("ping -c 4 www.google.com")
except IOError:
print "ISPerror: popen"
print("before read")
result = res.read()
res.close() # explicitly close the object
print ("after read: {}".format(result)
print ("exiting")
But this leaves you with the problem of knowing when the process has finished. And since res
just has type file
, your options are limited. I would instead move to using subprocess.Popen
Using subprocess.Popen
To use subprocess.Popen
, you pass your command in as a list
of strings. To be able to access the output of the process, you set the stdout
argument to subprocess.PIPE
, which allows you to access stdout
later on using file operations. Then, instead of using the regular os.wait()
method, subprocess.Popen
objects have their own wait
methods you call directly on the object, this also sets the returncode
value which represents the exit status.
import os
import subprocess
# list of strings representing the command
args = ['ping', '-c', '4', 'www.google.com']
try:
# stdout = subprocess.PIPE lets you redirect the output
res = subprocess.Popen(args, stdout=subprocess.PIPE)
except OSError:
print "error: popen"
exit(-1) # if the subprocess call failed, there's not much point in continuing
res.wait() # wait for process to finish; this also sets the returncode variable inside 'res'
if res.returncode != 0:
print(" os.wait:exit status != 0\n")
else:
print ("os.wait:({},{})".format(res.pid, res.returncode)
# access the output from stdout
result = res.stdout.read()
print ("after read: {}".format(result))
print ("exiting")