PyQT | QDesktopServices.openUrl 如果路径有空格则不起作用

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

我正在尝试使用 QDesktopServices 让系统打开指定的文件或文件夹。

下面的代码非常适合其中没有空格但否则无法执行的路径

def openFile(self):

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

带空格的路径的输出是

/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

我已验证指定的路径存在。

请帮忙

您需要使用 file:// url,否则 QUrl 会将路径视为网络 url 并将对其进行编码以便在该上下文中使用。所以试试这个:

url = QUrl.fromLocalFile(self.oVidPath)