How can you add an option so that you can select a folder or directory instead of a file extension type. For example, I want two options, a zip and a folder FileTypes option in my askopenfilename statement. I'd put filetypes=[('Zip File', '*.zip'), for a Zip file but what do I put for an actual directory or folder?
Asked
Active
Viewed 4,290 times
1 Answers
2
Try filedialog.askdirectory()
:
from tkinter import filedialog
dirname = filedialog.askdirectory()
For Python 2 use tkFileDialog.askdirectory()
.

mhawke
- 84,695
- 9
- 117
- 138
-
@import_os: There are many examples of using it [here](http://www.programcreek.com/python/example/16883/tkFileDialog.askdirectory). – martineau Apr 29 '17 at 03:45
-
But does this let you select zip files too? As the OP said "I want two options, a zip and a folder FileTypes option" – Taku Apr 29 '17 at 03:53
-
@abccd: As far as I know, there's nothing built-in that would do that—you'd need to create your dialog function to do that—which isn't that difficult, see this [documentation](http://web.archive.org/web/20161102024158/http://effbot.org/tkinterbook/tkinter-dialog-windows.htm). – martineau Apr 29 '17 at 04:03
-
@import_os: Also see [**_Correct way to implement a custom popup tkinter dialog box_**](http://stackoverflow.com/questions/10057672/correct-way-to-implement-a-custom-popup-tkinter-dialog-box). – martineau Apr 29 '17 at 04:07
-
I knew about this but wanted a work around with askopenfilename. Oh well, I'll just implement a way to make it where I can tell the user if they chose an invalid file type. Thanks. – marianydev Apr 29 '17 at 19:20