基本上我想要做的是将命令行可执行文件(如coursera-dl)生成的输出放到文本控件中。但它(命令行可执行文件)使用logging.info打印输出,似乎子进程无法读取logging.info打印输出但是当logging.info函数更改为print时,wxpython能够读取输出cmd到textctrl。我用python27。我的代码来自网络上的一堆代码:
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 :(得分: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})