2017-11-18 212 views
-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 
+0

您是否嘗試讀取stderr而不是stdout的子進程輸出?據我所知,這些是應用程序可用的唯一兩個輸出流。如果數據不在一個,它必須在另一個。 –

+0

@Paul Cornelius。我有。它似乎與記錄不打印到其輸出到標準輸出。所以我需要弄清楚如何讓它打印到標準輸出。但我並不想修改命令行源代碼 – Boikem

回答

0

它似乎與閱讀stderr的輸出有關。因此將'stderr':subprocess.PIPE改爲'stderr':subprocess.STDOUT解決了這個問題。所以這裏是如何在ret部分的變化看起來像

ret.update({'stdin':subprocess.PIPE, 
      'stderr':subprocess.STDOUT,#here's the solution 
      'startupinfo':si, 
      'env':env})