我從https://pymotw.com/2/subprocess/Python的與子「1>&2」和stderr = STDOUT
這個代碼我不知道如何解釋的代碼,在check_output
與1>&2
輸出重定向到stderr,但在參數,stderr返回標準輸出stderr=subprocess.STDOUT
。
output = subprocess.check_output(
'echo to stdout; echo to stderr 1>&2; exit 1',
shell=True,
stderr=subprocess.STDOUT,
)
print "*****************"
print 'Have %d bytes in output' % len(output)
print output
運行代碼,打印命令不執行沒啥意思被捕獲。
是什麼代碼試圖完成?當我註釋掉stderr=subprocess.STDOUT
線
*****************
Have 20 bytes in output
to stdout
to stderr
不過,我:
編輯
從答案和評論,我可以運行該代碼來獲得
try:
output = subprocess.check_output(
'echo to stdout; echo to stderr 1>&2; exit 1',
shell=True, # No such file or directory error without, maybe 1>&2 requires shell=True
stderr=subprocess.STDOUT,
)
except subprocess.CalledProcessError as e:
print "*****************"
print 'Have %d bytes in output' % len(e.output)
print e.output
這個輸出有代替
to stderr
*****************
Have 10 bytes in output
to stdout
EDIT2
我測試了更多的stderr庫(https://github.com/sickill/stderred),它可以幫助shell以紅色顯示stderr中的字符。
當我執行這個代碼(註釋掉重定向),我可以看到黑色的,這意味着它使用標準輸出的to stderr
。
output = subprocess.check_output(
'echo to stdout; echo to stderr 1>&2; exit 1',
shell=True,
#stderr=subprocess.STDOUT,
)
由此,我猜(糾正我,如果我錯了)Python的check_output
方法打印出來的數據爲標準錯誤重定向到標準輸出,使其打印出錯誤信息到標準錯誤。
即使我刪除了所有三個打印命令,我也得到了相同的輸出,所以我非常肯定打印命令是* n ot *執行。 – prosseek
@prosseek:是的,如果發生異常,除非你抓住它;不執行'print'語句,這是異常如何工作的 - 它們中斷正常的程序流。你可以添加'try:check_output(...),除了CalledProcessError爲e:print('例外:%s,捕獲的輸出:%s'%(e,e.output))' – jfs