Command shell access within Python

Stuart D. Gathman stuart at bmsi.com
Sun Nov 4 18:11:15 EST 2001


In article <87ady25fdf.fsf at lisboa.ifm.uni-kiel.de>, "Janko Hauser"
<jhauser at ifm.uni-kiel.de> wrote:

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

Oops, you are right.  I am a long way from a python guru...

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

I checked, and the "commands" module also creates a shell process to
parse the command.

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

It looks like os.spawn* is the way to go:

import os

def cmd(s):
  "I do not handle shell style I/O redirections or substitutions"
  sv = s.split()
  return os.spawnvp(os.P_WAIT,sv[0],sv)

error = cmd('make myproject')
if error == 0:
  cmd('ls')
  cmd('pj ci makefile')

-- 
	      Stuart D. Gathman <stuart at bmsi.com>
Business Management Systems Inc.  Phone: 703 591-0911 Fax: 703 591-6154
"Confutatis maledictis, flamis acribus addictis" - background song for
a Microsoft sponsored "Where do you want to go from here?" commercial.



More information about the Python-list mailing list