spawnlp, spawnvp and stdout-redirection
donn at u.washington.edu
Fri Aug 30 18:39:15 CEST 2002
Quoth Marc Saric <marc.saric at mpi-dortmund.mpg.de>:
| I wanted something similar to (csh)
| myprogram > logfile -option1 infile1 infile2 infile3
| which works pretty well on the console.
| If I try to do this with os.spawnlp(os.P_NOWAIT, 'myprogram',
| 'myprogram', '> logfile', '-option1', 'infile1', 'infile2', 'infile3')
| or similar ('>', 'logfile' or 'myprogram > logfile') it fails.
| I have no clue how to redirect output while directly calling the
| program. It works again with a little hack (a csh-script which takes
| everything as an argument, and is called by the python-script), but it
| is a bit ugly.
Well, it would be ideal if spawnlp() would do this for you, because
it's most efficiently accomplished between the fork() or vfork() system
call that creates the new process, and the execve() system call that
loads and executes the new program. At that point, prior to execve(),
you may simply open the new output file and dup2() it to unit 1 (output.)
You could write your own spawnlp() if you want. Or you can achieve
the same effect with only a small loss in elegance, if you open the
output file prior to spawnv, and then restore the old one afterwards.
fd = os.open(ofn, os.O_WRONLY|os.O_CREAT|os.O_TRUNC)
ofd = os.dup(1)
p = os.spawnv(os.P_NOWAIT, cmd, args)
Donn Cave, donn at u.washington.edu
More information about the Python-list