OS specific command in Python

Avell Diroll avelldiroll at yahoo.fr
Wed Jun 21 01:30:32 EDT 2006


diffuser78 at gmail.com wrote:
> I tried the following and it seemed to work
> 
> import os
> os.system('<system command here>')
> 
> Any comments....
> 

This is an simple way to proceed if you don't need your python script to 
know what happens to the launched process ...
When you need to :
* send some input to the command from the python script after it is launched
* get the output of the command in your python script
* get the pid associated to your command
* wait for your command to finish
* pipe some shell commands
* ...
you should use the subprocess module.

Here is a quick example from the Python Reference Library :
http://docs.python.org/lib/node242.html

##Shell Script :
output=`dmesg | grep hda`


##Python Script :
from subprocess import Popen
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]



More information about the Python-list mailing list