Hamza Bodla
Hamza Bodla

Reputation: 13

How to access a desired path with filedialog.askopenfilename() in tkinter

i have a simple function that i want to use to open a file dialog.

def open_music():
            filename = filedialog.askopenfilename()

it by default opens my documents directory. i want it to allow me to access a music folder with in my D drive.

Upvotes: 0

Views: 3646

Answers (2)

Eshita Shukla
Eshita Shukla

Reputation: 811

Yes, you are almost there. Just give the value of initial directory (starting directory) using the initialdir attribute. Following is how you do it:

# I am just assuming that 'D:\Music' is the path to the directory
filename = filedialog.askdirectory(initialdir='D:\Music')

If you get an error in the directory name above, try the following:

filename = filedialog.askdirectory(initialdir='D://Music')

Hope that helps!

Upvotes: 1

Partho63
Partho63

Reputation: 3117

Try This:

from tkinter import *
from tkinter import filedialog

root = Tk()

def open():
    filename = filedialog.askopenfilename(initialdir='D:\Music', title="Select Music")
    print(filename)

button = Button(root, text="Open Music Folder in D Drive", command=open)
button.pack()

root.mainloop()

Upvotes: 0

Related Questions