[英]saving subprocess output with tkinter
我的代码与子进程:
def GO():
my_sub=subprocess.Popen(['exe file','files in folder'],stderr=STADOUT,stdout=PIPE)\
.communicate()[0]
my_sub=my_sub.splitlines()
for lines in my_sub:
GO.a= lines
print GO.a
在印刷版GO.aa中具有:
1
2
3
4
在保存按钮中:
def save():
type = [('file', '*.txt')]
name = filedialog.asksaveasfile(type=ftypes, mode='w', defaultextension=".xxx")
name.write(GO.a)
name.close()
在保存的文件中,我有:
1
所以只有第一行,而不是所有行
如何保存它们或def GO()
所有输出?
编辑:(评论后):
def GO():
my_sub=subprocess.check_output(['exe file','files in folder'],stderr=STADOUT)
output=my_sub.splitlines()
for lines in output:
GO.a= lines
print GO.a
在保存按钮输出我收到只有一条线(最后一个4
) print GO.a
效果很好,也许我有保存按钮部分坏事?
尝试
def GO():
my_sub=subprocess.Popen(['exe file','files in folder'],stderr=STADOUT,stdout=PIPE)\
.communicate()[0]
lines = my_sub.splitlines()
GO.a = my_sub
for line in lines:
print line
在大多数情况下,将所有行分配给同一变量即可。
对于您当前的需求, Popen
过于Popen
。 当您想以更复杂的方式与子流程进行交互时,这很有用。
要仅将输出+错误流合并在一起,请使用check_output
,以获取更简单,更短的代码。 此函数将GO.a
设置为输出分割线:
def GO():
output = subprocess.check_output(['exe file','files in folder'], stderr=subprocess.STDOUT)
GO.a = output.splitlines()
如果您使用的是python 3并且需要文本,请使用
GO.a = output.decode().splitlines()
代替
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.