[英]Python subprocess and running script on directory
Im trying to run subprocess. 我试图运行子进程。 Im running a python file on a directory (to convert each file in the directory.) The convertor works and ive been implementing this into a gui(PYQT4).
我在目录上运行一个python文件(以转换目录中的每个文件。)转换器工作,并且已将其实现为gui(PYQT4)。 Here is what I got so far:
这是到目前为止我得到的:
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)
The error im getting is .. 我得到的错误是..
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
Any help for a beginner is appreciated :) 任何初学者的帮助,表示赞赏:)
From the comments to the question, the line: 从评论到问题,该行:
self.convertButton.clicked.connect(self.convertfile)
will send False
to the convertfile
method when the button is clicked, which is why you see that error. 单击按钮时,会将
False
发送到convertfile
方法,这就是您看到该错误的原因。
You need to add some code to convertfile
which gets the directory path from the selected item in the list-widget. 您需要向
convertfile
添加一些代码,该代码将从list-widget中的所选项目获取目录路径。 Something like: 就像是:
item = self.listWidget.currentItem()
if item is not None:
directory = unicode(item.text())
subprocess.call(['python', 'Createxmlfromcsv.py', directory])
But note that you are not storing the full directory path in the list-widget, and so the subprocess call may fail. 但是请注意,您没有将完整目录路径存储在list-widget中,因此子流程调用可能会失败。 You should really add items to the list-widget like this:
您确实应该像这样将项目添加到列表小部件:
self.listWidget.addItem(os.path.join(directory, file_name))
在“ subprocess.call(['python','Createxmlfromcsv.py',directory],shell = True)”中,“ directory”变量不是字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.