[Tutor] subprocess output

Evert Rol evert.rol at gmail.com
Wed Jul 28 09:09:37 CEST 2010


> #!/usr/bin/env python
> import subprocess
> import sys
> 
> dom = sys.argv[1]
> switch = sys.argv [2]
> answer = subprocess.call("whois " + dom, shell=True)
> 
> Execute the above (whatever.py -example.com -a1) prints the entire WHOIS
> output, but I just want it saved to the variable 'answer', nothing more.

create a subprocess.Popen object instead, and use communicate().
Use subprocess.PIPE for the stdout and stderr arguments (and use a list for the executable + arguments: ["whois", dom], leaving out the shell argument).
Read the library docs: see example 17.1.3.2.


> Changing 'shell=True' to 'shell=False' raises an exception and removing
> the 'shell=' altogether is troublesome as well.

shell=False is the default



More information about the Tutor mailing list