我正在使用Popen調用一個不斷將其stdout和stderr寫入日誌文件的shell腳本。有什麼辦法可以連續(到屏幕上)同時輸出日誌文件,或者讓shell腳本同時寫入日誌文件和stdout?Python Popen:同時寫入標準輸出和日誌文件
我基本上想要做這樣的事情在Python:
cat file 2>&1 | tee -a logfile #"cat file" will be replaced with some script
同樣,這種管標準錯誤/標準輸出一起三通,它都寫入到標準輸出和我的日誌文件。
我知道如何將stdout和stderr寫入Python中的日誌文件。當我被困在是如何把這些複製到屏幕:
subprocess.Popen("cat file", shell=True, stdout=logfile, stderr=logfile)
當然我可以做這樣的事情,但有什麼辦法做到這一點沒有三通和外殼文件描述符重定向?:
subprocess.Popen("cat file 2>&1 | tee -a logfile", shell=True)
相關:[?Python的子進程讓孩子的輸出到文件和終端(http://stackoverflow.com/q/4984428/4279) – jfs 2013-09-23 17:12:49