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 stderrwas printed. Why isn'tto stdoutprinted out first since it appeared first?Why is the
CompletedProcessobject only holding on toto stderrand notto stdout?- If I understand the cheatsheet's portion below, why isn't
to stderrsent to the standard error stream if it's file descriptor is2?
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.