[Tutor] Invoking bash from within a python program

Vinay Reddy vinayvinay at gmail.com
Sat Aug 13 19:29:30 CEST 2005


> I'm sure that somewhere in the dim and distant past this was asked, but
> I'll be b***ered if I can find reference to it, and google searches
> haven't been too illuminating yet. Anyway, how do I call bash to run a
> program - e.g. slocate - from within a python program. I believe that I
> have to import the OS first (or do I?) and I was thinking something like:
> 
> ...
> sh slocate file_name > python_defined_list

You can directly run slocate (or any other program) using the
os.popen* command. Refer to:
http://docs.python.org/lib/os-newstreams.html.
And you do need to import the os module.

The function call will be something like:
inStream, outStream = os.popen2("slocate file_name")

You can then read the output from outStream.

HTH,
Vinay Reddy


More information about the Tutor mailing list