0

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). 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 :)

jfs
  • 399,953
  • 195
  • 994
  • 1,670
Anekdotin
  • 1,531
  • 4
  • 21
  • 43
  • why are you using shell=True? – Padraic Cunningham Sep 21 '15 at 20:29
  • what is `type(directory)`? [don't use a list argument and `shell=True` on Unix](http://stackoverflow.com/q/2400878/4279). – jfs Sep 21 '15 at 20:31
  • ok removed shell = True :/ The directory is for all csv files in a directory. Want to run the script on them and convert them using a GUI and button – Anekdotin Sep 21 '15 at 20:33
  • i don't think `QtGui.QFileDialog.getExistingDirectory` returns what you think it does – Padraic Cunningham Sep 21 '15 at 20:34
  • Im not a PYQT expert by no means, i presumed it would select a directory – Anekdotin Sep 21 '15 at 20:36
  • If you `print(directory)` you will see for yourself – Padraic Cunningham Sep 21 '15 at 20:37
  • ahh it printed the path – Anekdotin Sep 21 '15 at 20:40
  • i changed directory to file_name in convertfile..still no luck :( – Anekdotin Sep 21 '15 at 20:41
  • how are you calling `convertfile`? Also you can simply pass the content of `glob.iglob(os.path.join(directory,"*.csv"))` to find all csvs – Padraic Cunningham Sep 21 '15 at 20:45
  • # Open a FILE and append to screen self.selectFileButton = QtGui.QPushButton('Select Files', self) self.selectFileButton.move(135, 200) self.selectFileButton.clicked.connect(self.selectFile) # convert files self.convertButton = QtGui.QPushButton('Convert!', self) self.convertButton.move(350,350) self.convertButton.clicked.connect(self.convertfile) – Anekdotin Sep 21 '15 at 20:48
  • # Open a FILE and append to screen self.selectFileButton = QtGui.QPushButton('Select Files', self) self.selectFileButton.move(135, 200) self.selectFileButton.clicked.connect(self.selectFile) # convert files self.convertButton = QtGui.QPushButton('Convert!', self) self.convertButton.move(350,350) self.convertButton.clicked.connect(self.convertfile) – Anekdotin Sep 21 '15 at 20:48
  • How are you passing the directory? – Padraic Cunningham Sep 21 '15 at 20:58
  • I was doing it as file_name. I wanted the script to be dynamic, rather than absolute paths. Right now I am trying absolute paths to see if I can get it to work – Anekdotin Sep 21 '15 at 21:06

2 Answers2

2

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.

You need to add some code to convertfile which gets the directory path from the selected item in the 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. You should really add items to the list-widget like this:

    self.listWidget.addItem(os.path.join(directory, file_name))
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
-1

In "subprocess.call(['python', 'Createxmlfromcsv.py', directory], shell=True)", the 'directory' variable is not a String.

AZV
  • 119
  • 4