Python function to choose a file in my PC
You should use askopenfilename
. With askdirectory
you will just get the directory name, so:
from tkinter import filedialogdef choose_file(self): root = Tk() root.withdraw() filetypes=[('DOCX','*.docx'),('PDF','*.pdf')] # Required file types path = filedialog.askopenfilename(title='Choose file',filetypes=filetypes) # os.chdir(path) There is no need of this now, as specific files will be only selected
If you want to choose multiple files then use askopenfilenames
instead or add an additional option to askopenfilename
, like:
path = filedialog.askopenfilename(title='Choose file',filetypes=filetypes,multiple=True)