[Tutor] Invoking bash from within a python program

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sat Aug 13 22:03:31 CEST 2005



On Sat, 13 Aug 2005, Vinay Reddy wrote:

> > 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.


Hi Joe,

Also, if you have a recent version of Python (Python 2.4), the
'subprocess' module might be worth a look:

    http://www.python.org/doc/lib/module-subprocess.html



> > This is to populate a list which would then become the filename list
> > for the remove file command to iterate through sequentially and
> > delete, as in:
> >
> > for i in python_defined_list:
> >        rm -fr i


'shutil' and its rmtree() function may be helpful for you:

    http://www.python.org/doc/lib/module-shutil.html

Good luck!



More information about the Tutor mailing list