pipes like perl
Donn Cave
donn at u.washington.edu
Wed Aug 24 15:01:21 EDT 2005
In article <i%IOe.95826$fm.6463957 at news4.tin.it>,
"max(01)*" <max2 at fisso.casa> wrote:
> in perl i can do this:
...
> but i do not know how to do it in python, because "if *command*:" gives
> syntax error.
>
> moreover, if i use
...
> it doesn't work, since "*do_something*" and *do_something_more* are
> always executed (it seems like
>
> MYPIPE = os.popen("*some_system_command*", "r")
>
> does not raise any exception even if *some_system_command* does not
> exist/work...
Just to address this last point -- if you're running 2.4,
you can get this through the subprocess module. With its
popen equivalent, something like
subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout
will raise an exception if the command is not found. The
command in this case would specified as an argv list, not
a shell command.
The basic problem is that you have to fork first, then
exec, and by the time the forked interpreter finds out
that the exec didn't work, its parent has gone on to
do the I/O it's expecting. I think subprocess gets
around that, on UNIX, with a trick involving an extra
pipe, that would work only on UNIX.
Donn Cave, donn at u.washington.edu
More information about the Python-list
mailing list