Masthead

Getting File Paths from the User

File Dialog

Periodically, you'll want to get the path to a file from the user. The PyQt library includes a function to display the operating systems Open File Dialog. The code below will display this dialog and then return the path the user selected:

from PyQt5.QtWidgets import *


# We have to create an application object for the FileDialog to appear to the user
TheApplication = QApplication([])


# Call QFileDialog to display the open file dialog and return a file path
TheFilePath,OtherInformation = QFileDialog.getOpenFileName(QWidget(), 'Open File', '/')
print(TheFilePath)

 

© Copyright 2018 HSU - All rights reserved.