Command shell access within Python

Janko Hauser jhauser at ifm.uni-kiel.de
Sun Nov 4 17:40:44 EST 2001


"Stuart D. Gathman" <stuart at bmsi.com> writes:

> In article <3BE5A1A7.75C7EC9C at attglobal.net>, "Pierre Rouleau"
> <pieroul at attglobal.net> wrote:
> 
> > Actually, i knew that i can execute a single program inside a command
> > shell and get the return code. What i want is to execute a large set of
> > commands inside the same shell process and being able to get the return
> > code for exach one of the calls.
> > 
> > For example,  i would like to be able to do someting like:
> > 
> > myShell = Process('cmd')
> > error = myShell.execute('make myproject') if (error == 0) :
> >     myShell.execute('ls')
> >     myShell.execute('pj ci makefile')
> > 
> > 
> > I want to be able to issue a whole list of line commands whithout having
> > to create a new process on every one of them.
> 
> When you run commands in a traditional shell script, it creates a new process
> for every one of them!
> 
> The equivalent of the above is:
> 
> #!/usr/bin/python
> import os
> error = os.system('make myproject')
> if error == 0:
>   os.system('ls')
>   os.system('pj ci makefile')
> 
> ...
> 
> In both cases, a new process is created for each command.
> 
AFAIK this is not right, as for the os.system() call first a shell is
started and then the process (ls) is started in this shell.

A better way is to use the commands module. It wraps os.popen and I'm
not sure if this also didn't open a shell beforehand.

Pure ways to start a new process are wrapped in the os module as 
os.exe?? os.fork and os.spawn??. But using these is more complicated
(io handling, finding the path of the executable etc.)

HTH,
__Janko

-- 
  Institut fuer Meereskunde             phone: 49-431-597 3989
  Dept. Theoretical Oceanography        fax  : 49-431-565876
  Duesternbrooker Weg 20                email: jhauser at ifm.uni-kiel.de
  24105 Kiel, Germany



More information about the Python-list mailing list