get the return code when piping something to a python script?
Jeff Schwab
jeffrey.schwab at rcn.com
Tue Aug 16 23:07:23 EDT 2005
mhenry1384 wrote:
> On WinXP, I am doing this
>
> nant.exe | python MyFilter.py
>
> This command always returns 0 (success) because MyFilter.py always
> succeeds.
...
> How do I set the return code from MyFilter.py based on the return of
> nant.exe? Is this possible? I have googled around for an hour wihtout
> success. I understand that I could have MyFilter.py call "nant.exe",
> but for various reasons, that's not idea for my situation.
It's probably not possible. The reason is that the left-most process in
the pipe is not guaranteed to exit before the right-most process, so in
general, there may not be any return code to get. If you're not too
attached to cmd.exe, you might want to try a different shell that can
provide a similar effect to what you requested. For example, in bash,
you might set the pipefail option.
$ ls nonesuch | ./return_zero.py
ls: nonesuch: No such file or directory
$ echo $? # Print the status of the last foreground pipe.
0
$ set -o pipefail
$ ls nonesuch | ./return_zero.py
ls: nonesuch: No such file or directory
$ echo $?
2
More information about the Python-list
mailing list