Python | Get file and folder paths in GUI format (tkinter.filedialog)

Do you find it very tedious to type in file paths in Python?

By using tkinter, you can read file paths by simply selecting files with the mouse!

This article explains how to select multiple files from Explorer and get their file paths in GUI format.

Table of Contents

Explain all functions of tkinter.filedialog

tkinter.filedialog handles file relationships in a GUI format.

tkinter.filedialog
Parameters
  • parent : the window to place the dialog on top of
  • title : the title of the window
  • initialdir : the directory that the dialog starts in
  • initialfile : the file selected upon opening of the dialog
  • filetypes : a sequence of (label, pattern) tuples, ‘*’ wildcard is allowed
  • defaultextension : default extension to append to file (save dialogs)
  • multiple : when true, selection of multiple items is allowed

The keyword arguments are applicable to the classes and functions listed below.

Functions
Official Documentation

Get a single file path with askopenfilename (title, initialdir)

askopenfilename can get the selected file path as a string

However, only one file can be selected.

Also, title (window title) and initialdir (starting directory) are specified as arguments.

Note that the plural s changes whether the file selection is singular or plural

path = filedialog.askopenfilename(title='Useful-Python', initialdir='C:/Users/Documents/')
print(path)
# C:/Users/Documents/230430_person_info_west.csv

Get multiple file paths with askopenfilenames (initialfile, filetypes)

askopenfilenames allows you to get the selected file paths as an array (tuple) of strings

It also takes two arguments: initialfile (initially selected file) and filetypes (selectable file types).

Separate multiple filetypes with a semicolon ex) *png; *jpg; *jpeg

path = filedialog.askopenfilenames(
    initialfile='230430_person_info_west.csv', 
    filetypes=[('CSVファイル', '*.csv')]
    )
print(path)
# ('C:/Users/Documents/230430_person_info_west.csv',)

Get a single folder path in askdirectory

askdirectory can get the selected folder path as a string

path = filedialog.askdirectory()
print(path)
# C:/Users/Documents

Make it a function and use it many times

Since the function to retrieve a file path is used in all kinds of situations, let’s make it a function that can be used just by calling it!

Function to get multiple file paths

The following code is a function to get multiple file paths with askopenfilenames.

The function name is read_filepathes

def read_filepathes() -> tuple:
    typ = [('CSV,Text File', '*.csv; *.txt')]
    dir = r'C:'
    return filedialog.askopenfilenames(filetypes=typ, initialdir=dir)

Execute function

The function is executed in read_filepathes() and the return value specified in return is assigned to pathes

This is exactly the array of file paths

pathes = read_filepathes()
print(pathes)
# ('C:/Users/Documents/230430_person_info_west.csv',)

References

Official documentation for Tkinter Dialogs

I hope you will share it with me!

Comments

To comment

Table of Contents