Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does QFileDialog use slash instead of backslash?

Why "\" and "/" are mixed?

os.getcwd() emits backslash string.

On the other hand, QFileDialog emits forward slash string.

Why?

Example

Please execute this sample code.

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()

Result (on my occasion)

from os.getcwd(): J:\

from QFileDialog: C:/Users/******/setup.py

like image 348
Haru Avatar asked Aug 22 '18 01:08

Haru


People also ask

What is the difference between a slash and a backslash?

Summary: The Backslash and Forward Slash Make sure to remember the following: The backslash (\) is mostly used in computing and isn't a punctuation mark. The forward slash (/) can be used in place of “or” in less formal writing. It's also used to write dates, fractions, abbreviations, and URLs.

Why does Windows use backslash instead of forward slash?

The reason Microsoft is backwards on this goes back to MS-DOS 2.0 (DOS 1.0 had no directory hierarchy), which used a backslash to stay compatible with Dos 1.0 commands, which used slash for command line switches.

How do I change backward slash to forward slash in Python?

The correct way would be s. replace('/', '\\') .

What is the purpose of the backslash (\) in the file path?

Windows traditionally uses the backslash ( \ ) to separate directories in file paths. (For example, C:\Program Files\PuppetLabs .)


1 Answers

This is because QFileDialog uses forward slashes regardless of OS. This makes it easier to write path handling code.

You can use os.path.normpath to converts forward slashes to backward slashes in a path on Windows.

like image 145
jpeg Avatar answered Sep 19 '22 01:09

jpeg