Wednesday, November 2, 2011

Running shell commands in Python

You can run shell commands in Python using the "os" library:

import os
os.system("command") 

If we need to capture the stdout or the stderr outputs in the python code, we need to use subprocesses.

from subprocess import Popen, PIPE, STDOUT

cmd = 'echo "Hello World"'
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
output = p.stdout.read()
print output

To stream the output in realtime:

import sys
from subprocess import Popen, PIPE, STDOUT

cmd = 'echo "Hello World"'
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
for line in iter(p.stdout.readline, ''):
    sys.stdout.write(line)

In few cases, we might need to stop/kill the subprocess opened using Popen. Such cases arise when we need to execute some system command from python, but do not want that command to take more than 30 seconds/1 minute. One dumb way to make this stopwatch/timer work is to run a while loop in the main python program. The code looks like:

import time
from subprocess import Popen, PIPE, STDOUT

p = Popen('COMMAND', shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT,close_fds=True)
time_start = time.time() 
seconds_passed = 0 
while(not p.poll() and seconds_passed<30):
    seconds_passed = time.time() - time_start 
if(seconds_passed>=30):    
    p.kill()   
else:
    output = p.stdout.read()


Reference:


No comments:

Post a Comment