In PyQt, how does one display a file browser that shows and selects only directories (not files)?
And how does one retrieve the name of the selected directory?
To find the current working directory in Python, use os. getcwd() , and to change the current working directory, use os. chdir(path) .
From inside your QDialog/QWidget class, you should be able to do:
file = str(QFileDialog.getExistingDirectory(self, "Select Directory"))
Just as simple as that:
folderpath = QtWidgets.QFileDialog.getExistingDirectory(self, 'Select Folder')
Here, self
represents the parent window usually the QMainWindow
object.
filepath = QtWidgets.QFileDialog.getOpenFileName(self, 'Hey! Select a File')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With