Running shell programs from Python

Donn Cave donn at u.washington.edu
Tue Apr 18 18:58:36 CEST 2000


Quoth seh at ffi.no:
| I have just started with Python and have some questions.. I have some
| compiled c programs that I normally run from the shell. I can use | to
| pipe the output from one as input to the next. I would like to call the
| programs from Python. Could someone please indicate how this is done?
| Any references?

The references are mainly UNIX man pages pipe(2), fork(2), fcntl(2)
(look for dup2), exec(2) and wait(2).

Basically, the sequence is repeat ( open pipes, fork, close pipes )
After a fork, the parent and child both have both ends of each pipe
open, and it's important to close the unused "write" ends so the 
pipe will generate an EOF when its real writer exits.

Each of the forks will call execve() or a related function to invoke
the program, after forking if that's necessary.  Use dup2() to open
units 0 and 1 on the read and write pipes respectively, and close the
original units.  Trap any possible exceptions in the fork, so control
doesn't return to the caller in the fork.

While I do this with single commands, I personally don't ever do it
with a complex pipeline, because it's kind of hard to get right and
the UNIX shell just does a much better job.  If you just want to run
the shell from Python, as someone else already has mentioned, use
os.system(), os.popen() and various cool things in the popen2 module.

	Donn Cave, University Computing Services, University of Washington
	donn at u.washington.edu



More information about the Python-list mailing list