-1
基本上我想要做的是將命令行可執行文件(如coursera-dl)生成的輸出轉換爲文本控件。但它(命令行可執行文件)使用logging.info輸出它的輸出,似乎子進程無法讀取logging.info輸出,但當logging.info函數更改爲打印時,wxpython能夠讀取輸出cmd到一個文本控件。我使用python27。我從一堆代碼放在網上的代碼:在wxpython文本控件中輸出由logging.info生成的輸出
self.courses_list = ""
def execute(self,command,textctrl):
#clear the textctrl
#try:
textctrl.SetValue("")
si=subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
process = subprocess.Popen(command,stdout=subprocess.PIPE,**self.subprocess_args(False))
output = ''
self.out=[]
# Poll process for new output until finished
for line in iter(process.stdout.readline, ""):
textctrl.AppendText(line)
output += line#.strip().decode('utf-8')
self.courses_list+=line
print(line)
self.out.append(line)
process.wait()
exitCode = process.returncode
if (exitCode == 0):
return output
else:
raise Exception(command, exitCode, output)
def subprocess_args(self,include_stdout=True):
if hasattr(subprocess, 'STARTUPINFO'):
si=subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
env = os.environ
else:
si=None
env=None
if include_stdout:
ret={'stdout:':subprocess.PIPE}
else:
ret={}
ret.update({'stdin':subprocess.PIPE,
'stderr':subprocess.PIPE,
'startupinfo':si,
'env':env})
return ret
您是否嘗試讀取stderr而不是stdout的子進程輸出?據我所知,這些是應用程序可用的唯一兩個輸出流。如果數據不在一個,它必須在另一個。 –
@Paul Cornelius。我有。它似乎與記錄不打印到其輸出到標準輸出。所以我需要弄清楚如何讓它打印到標準輸出。但我並不想修改命令行源代碼 – Boikem