Remeber last saved directory with QFileDialog

Heads up! You've already completed this tutorial.

Santiago_Pineda | 2020-06-04 12:29:36 UTC | #1

I have a GUI with a field for file name ( self.ui.lineEditFileName) and I need that the file name goes automatically to the file name field in the QFileDialog and set it to always open in D:\Data. However I want to be able to get to the last directory location where the last file was saved.

If i remove the 3rd argument of the function this exactly what happens but then i lose the file name signal and the field is blank.

Thanks for any help.

fileName, _ = QFileDialog.getSaveFileName(self, "Save Measurement", 'D:\Data\' + self.ui.lineEditFileName.text(), "(*.csv)", options=options)


martin | 2020-06-04 20:59:10 UTC | #2

Hey @Santiago_Pineda welcome to the forum! [quote="Santiago_Pineda, post:1, topic:246"] Get it to always open in D:\Data. However I want to be able to get to the last directory location where the last file was saved. [/quote]

Do you mean that instead of going to D:\Data you want it to open at the last folder (if there is one)?

In that case what I would do is save the folder of the previous filename, e.g.

python
self.active_folder = os.path.dirname(fileName)

You can set self.active_folder = 'D:\Data\' by default if you like (or set it to None and check for this, defaulting to not passing anything in).

To use this you would then do --

python
path = os.path.join(self.active_folder, self.ui.lineEditFileName.text())
fileName, _ = QFileDialog.getSaveFileName(self, “Save Measurement”, path, “(*.csv)”, options=options)

Does that make sense? Let me know if I've misunderstood!


Santiago_Pineda | 2020-06-05 14:15:31 UTC | #3

Thanks for the reply. This is exactly what i meant. What was confusing is that if you replace the 3rd argument of the getSaveFileName method for the empty string "", then the dialog will remember the last directory every time I save, but of course i would lose the file name from my GUI lineEditFileName field. This is how it is working as I intended after your feedback:

In the init function (Note that the \ has to be escaped, otherwise it includes the directory path as part of the file name):

self.active_folder = 'D:\\Data\\'

In the save function:

python
path = os.path.join(self.active_folder, self.ui.lineEditFileName.text())
fileName, _ = QFileDialog.getSaveFileName(self, "Save Measurement", path, "(*.csv)", options=options)
self.active_folder = os.path.dirname(fileName)

Thanks again Martin


The complete guide to packaging Python GUI applications with PyInstaller.
[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

Remeber last saved directory with QFileDialog was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.