简体   繁体   中英

Read from stdin, stderr in Python

I have two programs: p1 and p2. I run p1 | p2. I know how to read stdout from p1 in p2 stdin. How can I do the same with stderr.

Thanks, Nicolas

A pipe ( | ) in most (or all) shells takes the stdout of a command and passes it to the second command.

Taking the stderr from the first command can therefore only be done on the command line. In bash you'd do it this way:

p1 2>&1 | p2

This tells bash to forward stderr to stdout. Both are then piped to p2.

Alternatively you can make p1 print everything to stdout only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM