Python 3 - Tkinter - askopenfilename - Filter filenames trough regEx (Not by extension)

Viewed 759

Is it possible to filter the filenames we see when we browse for a file using tkinter askopenfilename ? I don't need to filter by extension, but by a specific pattern in the filename.

For example I have hundred of files in one folder, most named "Chapter_XXX - Title" and only one file named "Book". I would like to display only the files that start with Book.

Is such a thing possible?

Thanks Martin, that worked. These type of examples should be included in the documentation.

The code I needed:

fileName = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("Books","Book*.*"),("All files","*.*")))
1 Answers

Just like the extension, you can define strings that should be contained in the filename using the filetypesargument.

So, analogous to the extension tuple ('Excel Spreadsheet','.xlsx'), you can specify to get all .xlsx-files, you can define ('Book Files','book*.*'). This would result in a list where the string "book" appears anywhere in the filename.

Related