我试图运行子进程。我在一个目录上运行一个python文件(转换目录中的每个文件。)转换器工作,并已将其实现为gui(PYQT4)。这是我到目前为止所得到的:
def selectFile(self):
self.listWidget.clear() # In case there are any existing elements in the list
directory = QtGui.QFileDialog.getExistingDirectory(self,
"Pick a folder")
if directory:
for file_name in os.listdir(directory):
if file_name.endswith(".csv"):
self.listWidget.addItem(file_name)
print (file_name)
def convertfile(self, directory):
subprocess.call(['python', 'Createxmlfromcsv.py', directory], shell=True)
我得到的错误是..
Traceback (most recent call last):
File "/Users/eeamesX/PycharmProjects/Workmain/windows.py", line 162, in convertfile
subprocess.call(['python', 'Createxmlfromcsv.py', directory], shell=True)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
TypeError: execv() arg 2 must contain only strings
对于初学者的任何帮助表示赞赏:)
答案 0 :(得分:2)
从评论到问题,行:
self.convertButton.clicked.connect(self.convertfile)
单击按钮时,会向False
方法发送convertfile
,这就是您看到错误的原因。
您需要向convertfile
添加一些代码,以获取list-widget中所选项目的目录路径。类似的东西:
item = self.listWidget.currentItem()
if item is not None:
directory = unicode(item.text())
subprocess.call(['python', 'Createxmlfromcsv.py', directory])
但请注意,您没有在list-widget中存储完整的目录路径,因此子进程调用可能会失败。您应该将项目添加到list-widget中,如下所示:
self.listWidget.addItem(os.path.join(directory, file_name))
答案 1 :(得分:-1)
在" subprocess.call([' python',' Createxmlfromcsv.py',目录],shell = True)",'目录'变量不是String。