How to specify the file path in a tkinter filedialog?

Tkinter offers several built-in functions and class library methods to build components and user-actionable items of an application. filedialog is one of the tkinter modules that provides classes and library functions to create file/directory selection windows. You can use filedialog where you need to ask the user to browse a file or a directory from the system.

You can also specify the location of the directory from where a particular file should be picked up. To display the filedialog that starts from a particular location, use the initialdir = <location> argument in the static factory function askopenfilename(initialdir=<location>). This function creates a modal-like dialogbox and waits for the user's selection and returns the value of the selected file to the caller.

Basic File Dialog Example

Let us create an application which asks the user to select a file from the system directory ?

# Import required libraries
from tkinter import *
from tkinter import filedialog
from tkinter import ttk

# Create an instance of tkinter window
win = Tk()
win.geometry("700x350")
win.title("File Dialog Example")

def open_win_diag():
    # Create a dialog box with initial directory
    file_path = filedialog.askopenfilename(initialdir="C:/")
    if file_path:
        print(f"Selected file: {file_path}")

# Create a label widget
label = Label(win, text="Click the button to browse the file", font='Arial 15 bold')
label.pack(pady=20)

# Create a button to open the dialog box
button = ttk.Button(win, text="Open", command=open_win_diag)
button.pack(pady=5)

win.mainloop()

Specifying Different Initial Directories

You can specify different initial directories based on your requirements ?

import os
from tkinter import *
from tkinter import filedialog

root = Tk()
root.geometry("400x300")
root.title("Multiple Directory Options")

def open_desktop():
    desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
    file_path = filedialog.askopenfilename(initialdir=desktop_path)
    if file_path:
        print(f"Desktop file: {file_path}")

def open_documents():
    documents_path = os.path.join(os.path.expanduser("~"), "Documents")
    file_path = filedialog.askopenfilename(initialdir=documents_path)
    if file_path:
        print(f"Documents file: {file_path}")

def open_current():
    current_path = os.getcwd()
    file_path = filedialog.askopenfilename(initialdir=current_path)
    if file_path:
        print(f"Current directory file: {file_path}")

Button(root, text="Open Desktop", command=open_desktop).pack(pady=10)
Button(root, text="Open Documents", command=open_documents).pack(pady=10)
Button(root, text="Open Current Directory", command=open_current).pack(pady=10)

root.mainloop()

Common Initial Directory Paths

Path Type Windows Example Cross-Platform Code
Home Directory C:/Users/Username os.path.expanduser("~")
Desktop C:/Users/Username/Desktop os.path.join(os.path.expanduser("~"), "Desktop")
Documents C:/Users/Username/Documents os.path.join(os.path.expanduser("~"), "Documents")
Current Directory Script location os.getcwd()

File Dialog with File Type Filters

You can also specify file type filters along with the initial directory ?

from tkinter import *
from tkinter import filedialog

root = Tk()
root.withdraw()  # Hide the main window

# Open dialog with initial directory and file type filters
file_path = filedialog.askopenfilename(
    initialdir="C:/",
    title="Select a file",
    filetypes=(
        ("Text files", "*.txt"),
        ("Python files", "*.py"),
        ("All files", "*.*")
    )
)

if file_path:
    print(f"Selected file: {file_path}")
else:
    print("No file selected")

root.destroy()

Output

Running the above code will display a window that contains the button widget. The button widget triggers the file dialog box, asking the user to browse the file from the system starting from the specified initial directory (C:/ in this case).

We have specified "initialdir=C:/" in the askopenfilename() function. Hence, it opens the C Drive as the initial directory.

Conclusion

Use the initialdir parameter in filedialog.askopenfilename() to specify the starting directory. Combine with os.path functions for cross-platform compatibility and file type filters for better user experience.

Updated on: 2026-03-26T18:53:07+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements