Tkinter open file window, file extension case sensitivity

Question:

I’m playing a bit with tkinter for one of my scripts and I have trouble using the filetypes argument for the askopenfilename() method.

INFILE = askopenfilename(filetypes = (("TEST files", "*.test"), ("all files", "*.*")))

This is working pretty good but the filter is case sensitive, is there any way to make it not ?
I’d like to be able to see all files with .test extension, no matter what the case is (aka: .teSt .TEST .test)

I’m pretty sure I don’t have to hard write every single combination, so if you have any idea of how to do this

Asked By: Plopp

||

Answers:

There is no built-in option to do that, but you can for example save the case sensitive extensions in a list and then refer to it:

from tkinter import filedialog
from tkinter import *


text_file_extensions = ['*.txt', '*.txT', '*.tXT',  '*.Txt', '*.TXt', '*.TXT', '*.tXt',
                        '*.TxT']
ftypes = [
    ('test files', text_file_extensions),
    ('All files', '*'),
]

root = Tk()
root.filename = filedialog.askopenfilename(initialdir="/", title="Select file",
                                           filetypes=ftypes)
print(root.filename)

Demo:

enter image description here

Answered By: Billal Begueradj
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.