I'm trying to call a shell script in python, but it keeps reporting broken pipe error (the result is OK, but i don't want to see the error message in STDERR). I have pinpointed the cause, and it can be reproduced as the following snippet:
subprocess.call('cat /dev/zero | head -c 10 | base64', shell=True)
AAAAAAAAAAAAAA==
cat: write error: Broken pipe
/dev/zero is an infinite stream, but the head -c 10 only reads 10 bytes from it and exits, then cat will get SIGPIPE because of the peer has closed the pipe. There's no broken pipe error message when i run the command in shell, but why python shows it?
subprocess.call('head -c 10 < /dev/zero | base64', shell=True)headandbase64, it may be too difficult to manage in pure Python. Yours solves the question as asked, which is of value.catcomplain about a broken pipe before, and have always relied on it exiting silently when the pipe is closed.