Skip to content
Advertisement

tkinter filedialog – how to get directory of selected file?

How can I get folder where is stored file selected from tkinter filedialog?

I want make something such this:

from tkinter import filedialog as fd
filename = fd.askopenfilename()
print(filename.directory)

Advertisement

Answer

You could use os.path.dirname for that.

from tkinter import filedialog as fd
import os

filename = fd.askopenfilename()
print(os.path.dirname(filename))
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement