为什么QFileDialog使用斜杠而不是反斜杠?

问题描述 投票:1回答:1

为什么“\”和“/”是混合的?

os.getcwd()发出反斜杠字符串。

另一方面,QFileDialog发出正斜杠字符串。

为什么?

请执行此示例代码。

from PySide import QtGui
from PySide import QtCore
import sys
import os

class DirectoryPrinter(QtGui.QWidget):
    def __init__(self,parent=None):
        super(DirectoryPrinter,self).__init__(parent=None)

        self.filedialog_pushbutton = QtGui.QPushButton("filedialog",self)
        self.connect(self.filedialog_pushbutton,QtCore.SIGNAL("clicked()"),self.filename_getter)

    def filename_getter(self):
        print("from os.getcwd()",os.getcwd())
        filename = QtGui.QFileDialog.getOpenFileName(self,"Select your file",os.path.expanduser("~"))[0]
        print("from QFileDialog",filename)


def main():
    try:
        QtGui.QApplication([])
    except Exception as e:
        print(22,e)
    directoryprinter = DirectoryPrinter()
    directoryprinter.show()

    sys.exit(QtGui.QApplication.exec_())
if __name__ == "__main__":
    main()

结果(在我看来)

来自os.getcwd()J:\

来自QFileDialogC:/Users/******/setup.py

python operating-system windows-10 pyside qfiledialog
1个回答
1
投票

这是因为QFileDialog uses forward slashes regardless of OS. This makes it easier to write path handling code

您可以使用os.path.normpath将正斜杠转换为Windows上路径中的反斜杠。

© www.soinside.com 2019 - 2024. All rights reserved.