我正在创建一个GUI,其中我从用户那里获得输入(文件名)。
然后我必须在该文件上运行一些shell脚本并分别显示每个脚本的输出。
我有点坚持使用子进程模块。每次我跑,都会引发错误。
另一件事是,我如何创建一个全局字符串变量,因为我无法访问我在另一个函数中使用的变量。
CODE:
import sys
from Tkinter import *
from tkFileDialog import *
import subprocess
import os
FileName = ""
FilePath = ""
def browse():
Tk().withdraw()
FilePath = askopenfilename(filetypes = (("ApkFiles","*.apk"),("All files", "*")))
print FilePath
Parts = FilePath.split("/")
FileName = Parts[-1]
name = str(FileName)
#print FileName
def process():
print FileName
#subprocess.call("cat ", FileName, shell=True)
#Content = open(self.filename,"r").readlines()
#print Content
#subprocess.call("command-name-here")
#output = subprocess.call(["/path/to/command", "arg1", "-arg2"])
#subprocess.Popen("ls", stdout=subprocess.PIPE, shell=True)
#subprocess.call ("ls")
subprocess.call (['cp filename /Home/dinesh/Base/ApkFiles/'])
subprocess.call (['cd /Home/dinesh/Base'])l
subprocess.call (['./AppSplit 0.1 0.1 0.8'])
Status = subprocess.call (["AppSplit.sh" ,"filename"])
#exit (Status)
# exit (0)
gui = Tk() #create an object
gui.title("Select an app to be clustered")
gui.geometry("700x400")
GuiLabel1 = Label(gui,text="Select an app to be clustered").grid(row=0 , column=4)
GuiLabel2 = Label(gui,text="ApkFile").grid(row=3 ,column=3)
bar=Entry(gui).grid(row=3, column=4)
button1= Button(gui, text="Browse", command = browse).grid(row=3, column=5)
button2= Button(gui, text="Cluster", command = process).grid(row=4, column=5)
gui.mainloop() #necessary for windows
答案 0 :(得分:0)
对于subProcess.call,您需要将参数作为每个字符串的字符串列表传递,而不是作为列表中的单个字符串。
subprocess.call (['cp', filename, '/Home/dinesh/Base/ApkFiles/'])
# or use Python
shutil.copy(filename, '/Home/dinesh/Base/ApkFiles/')
subprocess.call (['cd', '/Home/dinesh/Base'])
# or use python
os.chdir('/Home/dinesh/Base')
subprocess.call (['./AppSplit', '0.1 0.1 0.8'])
您可以将全局变量放在函数定义之外,并在函数
中使用它们全局关键字globalvar1 = "This is global"
# in the function
def myfunc():
global globalvar1