[Tutor] error when using using subprocess.popen in Python wrapper script

Peter Otten __peter__ at web.de
Mon May 7 09:44:16 CEST 2012


Rogelio wrote:

> If I want to write this command to a file, would this be the right format?
> 
> *********************************
> import subprocess
> 
> (all my variables defined okay)
> 
> perl_script=subprocess.call(['perl',perl_prog,ipfile,cmd,user,timeout,])
> 
> log=open('/tmp/pythonOutput.txt',w)
> log.write(subprocess.call(perl_script))
> *********************************
> 
> The program runs (and outputs stuff on the screen okay), but when I
> "cat /tmp/pythonOutput.txt", nothing is there.

The documentation is fairly clear:

"""
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
Run the command described by args. Wait for command to complete, then return 
the returncode attribute.
"""

> (While I'm not waiting for the entire program to run across all the IP
> addresses, I would think that something would be go out into that log
> file.)

No, you'll get an integer return code and ultimately a TypeError when you 
pass that to the file's write() method.

Read the docs on Popen.communicate()

http://docs.python.org/library/subprocess.html#subprocess.Popen.communicate




More information about the Tutor mailing list