Using subprocess Module
Revision as of 09:19, 15 September 2015 by PeterHarding (talk | contribs) (Created page with "=Source= Some examples of using the the subprocess module. <pre> #!/usr/bin/env python import subprocess def execute(command): tokens = command.split() print tok...")
Source
Some examples of using the the subprocess module.
#!/usr/bin/env python
import subprocess
def execute(command):
tokens = command.split()
print tokens
process = subprocess.Popen(tokens, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = ''
# Poll process for new output until finished
for line in iter(process.stdout.readline, ""):
print line,
output += line
process.wait()
exitCode = process.returncode
if (exitCode == 0):
return output
else:
raise Exception(command, exitCode, output)
execute('ping -c 23 127.0.0.1')