-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