pythonpyqtpyqt5qurlqdesktopservices

PyQT | QDesktopServices.openUrl Doesn't work if path has spaces


I am trying to use QDesktopServices to have system open the files or folders specified.

The code below works perfect for paths which doesn't have spaces in them but fails to execute if otherwise

def openFile(self):

    print self.oVidPath
    print "\n"
    url = QUrl(self.oVidPath)
    QDesktopServices.openUrl(url)
    self.Dialog.close()

and the output for paths with spaces is

/home/kerneldev/Documents/Why alcohol doesn't come with nutrition facts.mp4


gvfs-open: /home/kerneldev/Documents/Why%20alcohol%20doesn't%20come%20with%20nutrition%20facts.mp4: error opening location: Error when getting information for file '/home/kerneldev/Documents/Why%20alcohol%20doesn't%20come%20with%20nutrition%20facts.mp4': No such file or directory

I have verified that the path specified exists.

Please Help


Solution

  • You need to use a file:// url, otherwise QUrl will treat the path as a network url and will encode it for use in that context. So try this instead:

    url = QUrl.fromLocalFile(self.oVidPath)