簡體   English   中英

用tkinter保存子過程輸出

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

在保存按鈕輸出我收到只有一條線(最后一個4print 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM