[英]saving subprocess output with tkinter
my code with subprocess: 我的代码与子进程:
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
In print GO.aa have: 在印刷版GO.aa中具有:
1
2
3
4
In save button: 在保存按钮中:
def save():
type = [('file', '*.txt')]
name = filedialog.asksaveasfile(type=ftypes, mode='w', defaultextension=".xxx")
name.write(GO.a)
name.close()
In saved file i have : 在保存的文件中,我有:
1
so only 1st line, not all lines 所以只有第一行,而不是所有行
How to save them all, or all output from def GO()
? 如何保存它们或
def GO()
所有输出?
EDIT: (after comments): 编辑:(评论后):
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
In save button output i am receiving only one line (last one 4
) print GO.a
works well, maybe i have something bad in save button section? 在保存按钮输出我收到只有一条线(最后一个
4
) print GO.a
效果很好,也许我有保存按钮部分坏事?
Try 尝试
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
assigning all the lines to the same variable does that, most of the time. 在大多数情况下,将所有行分配给同一变量即可。
Popen
is overkill for your current needs. 对于您当前的需求,
Popen
过于Popen
。 It's useful when you want to interact with the subprocess in a more complex way. 当您想以更复杂的方式与子流程进行交互时,这很有用。
To just get the output+error streams merged together, use check_output
instead, for a simpler & shorter code. 要仅将输出+错误流合并在一起,请使用
check_output
,以获取更简单,更短的代码。 This function sets GO.a
to the output splitted lines: 此函数将
GO.a
设置为输出分割线:
def GO():
output = subprocess.check_output(['exe file','files in folder'], stderr=subprocess.STDOUT)
GO.a = output.splitlines()
if you're using python 3 and you need text, use 如果您使用的是python 3并且需要文本,请使用
GO.a = output.decode().splitlines()
instead 代替
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.