tkinter filedialog – how to get directory of selected file?

Tags: , , ,



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)

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))


Source: stackoverflow