Reputation: 1746
I have been going through the subprocess
module examples on Doug Helmann's PYMOPTW. Here's the code snippet that I have trouble with.
# subprocess_run_output_error.py
import subprocess
try:
completed = subprocess.run(
'echo to stdout; echo to stderr 1>&2; exit 1',
check=True,
shell=True,
stdout=subprocess.PIPE,
)
except subprocess.CalledProcessError as err:
print('ERROR:', err)
else:
print('returncode:', completed.returncode)
print('Have {} bytes in stdout: {!r}'.format(
len(completed.stdout),
completed.stdout.decode('utf-8'))
)
I understand that exit 1
is supposed to throw an error and the except clause is run.
to stderr
ERROR: Command 'echo to stdout; echo to stderr 1>&2; exit 1' returned non-zero exit status 1.
I don't get why to stdout
is not printed but to stderr
. Doesn't 1>&2
appear after echo to stdout
has been run?
For better understanding, I changed the code to see if I could get the else
portion to run so I switched it to exit 0
. When I did so, the output that I got was:
to stderr
returncode: 0
Have 10 bytes in stdout: 'to stdout\n'
I don't seem to understand what the 1>2
means despite going to cheatsheets.
Again to stderr
was printed. Why isn't to stdout
printed out first since it appeared first?
Why is the CompletedProcess
object only holding on to to stderr
and not to stdout
?
to stderr
sent to the standard error stream if it's file descriptor is 2
?n>&m # file descriptor n is made to be a copy of the output file descriptor
The other question that I found relatively close to this was this. However, it was comparing &>
and >&
. I couldn't make sense of the initial >&
so I felt even more confused.
Upvotes: 0
Views: 163
Reputation: 5762
completed.stdout
.stdout=subprocess.PIPE
, but no stderr=subprocess.PIPE
Upvotes: 1