drag-and-dropwxpythonpopenwx.textctrl

Wxpython drag and drop folder path, Popen not working with spaces on Windows


I have the below code which allows the user to drag and drop a folder to get the folder path. I then take this folder path and use it to pass through to a command line application in Windows using Popen. This all works fine except if there are spaces in the folder path, which then fails. I am currently getting round this by using win32api.GetShortPathName(folder_list) which shortens them to DOS 8.3 spec, but I would like to use the full absolute path. I know the command line application takes paths with spaces as I also use a batch file with drag and drop which works with spaces in paths. I have tried inserting escapes, etc, still no luck. How can I get this to work properly with full folder paths with spaces?

class SubmissionPane(wx.Panel):
    def __init__(self, parent, queue_control):
        wx.Panel.__init__(self, parent, -1)

        self.parent = parent
        self.queue_control = queue_control
        self.selected_folder = None

        self.txtTitle = wx.TextCtrl(self, pos=(125, 70), size=(215, 25), style= wx.SUNKEN_BORDER, value="Enter Series Title Here")
        self.txtTitle.Show(False)

        self.drop_target = MyFileDropTarget(self)
        self.SetDropTarget(self.drop_target)

    def SetSubmissionFolders(self, folder_list):
        """Called by the FileDropTarget when files are dropped"""
        print "Setting submission folders", folder_list
        self.tc_files.SetValue(','.join(folder_list))  
        self.selected_folders = folder_list

class MyFileDropTarget(wx.FileDropTarget):
    """"""
    def __init__(self, window):
        wx.FileDropTarget.__init__(self)
        print "Creating a drop file target..."
        self.window = window

    def OnDropFiles(self, x, y, filenames):
        self.window.SetSubmissionFolders(filenames)

I then submit this to Popen like this:

command1 = commandLineApplication + folder_list
process = Popen(command1, shell=True, stdin=PIPE)

Solution

  • You probably just need to put quotes around each of the file paths. That usually works. The win32api.GetShortPathName is a neat trick though.

    Here's one way to do it:

    n = ['"%s"' % x for x in folderlist]
    

    Then do the

    ','.join(folder_list)
    

    You mentioned in your code.