[英]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.