Interpret output from bash in Python's subprocess module
我一直在阅读DougHelmann的Pymoptw上的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # 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')) ) |
我知道
1 2 | to stderr ERROR: Command 'echo to stdout; echo to stderr 1>&2; exit 1' returned non-zero exit status 1. |
我不明白为什么
为了更好地理解,我修改了代码以查看是否可以运行
1 2 3 4 | to stderr returncode: 0 Have 10 bytes in stdout: 'to stdout ' |
我似乎不明白
又一次打印了
为什么
n>&m # file descriptor n is made to be a copy of the output file
descriptor
我发现另一个相对接近的问题是这个。但是,它比较了