How to get the absolute path of a file using tkFileDialog(Tkinter)?

Tkinter is a standard Python library used to create functional GUI applications. The tkinter.filedialog module provides functions for interacting with system files and directories, allowing users to select files through a dialog box.

When you select a file using filedialog.askopenfile(), you can get its absolute path using os.path.abspath(file.name). This returns the complete file path from the root directory, which is useful for file processing operations.

Syntax

import os
from tkinter import filedialog

file = filedialog.askopenfile(mode='r')
absolute_path = os.path.abspath(file.name)

Example

Here's a complete example that opens a file dialog and displays the absolute path of the selected file ?

# Import the required Libraries
from tkinter import *
from tkinter import ttk, filedialog
import os

# Create an instance of tkinter frame
win = Tk()

# Set the geometry of tkinter frame
win.geometry("700x350")

def open_file():
    file = filedialog.askopenfile(mode='r', filetypes=[('Python Files', '*.py'), ('All Files', '*.*')])
    if file:
        filepath = os.path.abspath(file.name)
        Label(win, text="The File is located at: " + str(filepath), font=('Arial', 11)).pack()

# Add a Label widget
label = Label(win, text="Click the Button to browse the Files", font=('Georgia', 13))
label.pack(pady=10)

# Create a Button
ttk.Button(win, text="Browse", command=open_file).pack(pady=20)

win.mainloop()

How It Works

The code works in the following steps:

  1. filedialog.askopenfile() opens a file selection dialog
  2. os.path.abspath(file.name) converts the file path to an absolute path
  3. The absolute path is displayed in a label widget

Alternative Approach Using askopenfilename()

You can also use askopenfilename() which directly returns the file path ?

from tkinter import *
from tkinter import filedialog
import os

win = Tk()
win.geometry("400x200")

def open_file():
    filepath = filedialog.askopenfilename(
        title="Select a file",
        filetypes=[('Python Files', '*.py'), ('Text Files', '*.txt'), ('All Files', '*.*')]
    )
    if filepath:
        absolute_path = os.path.abspath(filepath)
        print(f"Absolute path: {absolute_path}")

Button(win, text="Browse File", command=open_file).pack(pady=50)
win.mainloop()

Key Points

  • os.path.abspath() converts relative paths to absolute paths
  • filedialog.askopenfile() returns a file object with a name attribute
  • filedialog.askopenfilename() directly returns the file path as a string
  • Always check if a file was selected before accessing its properties

Conclusion

Getting the absolute path of a file in Tkinter is straightforward using os.path.abspath() combined with file dialog functions. This approach is essential for file processing applications where you need the complete file location.

Updated on: 2026-03-25T23:33:41+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements