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

标签 python operating-system windows-10 pyside qfiledialog

为什么“\”和“/”混在一起?

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:\

来自QFileDialog:C:/Users/******/setup.py

最佳答案

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

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

关于python - 为什么 QFileDialog 使用斜杠而不是反斜杠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51958626/

相关文章:

linux机器上可以查看页表吗

c# - 在 Windows 10 中编译的问题

vba - 可以在 Outlook 电子邮件中插入可折叠列表吗?

c++ - 抛出异常但未在 C++ 程序中捕获

python - 如何在 Cython 中测量毫秒级的时间?

python - 使用 numpy 数组函数更新元素

python - 如何在linux上通过python代码进入文件夹

python - pywinauto 等待窗口出现并发送按键

python - 包裹时如何防止seaborn FacetGrid重叠

linux - htop如何收集linux中的系统信息