Check if a File or Directory Exists using Python

You can use the following templates to check if a file or directory exists using Python:

(1) Check if a file exists using os.path.isfile:

import os.path
file_exists = os.path.isfile(r'path where the file is stored\file name.file extension')
print(file_exists)

(2) Check if a directory exists using os.path.isdir:

import os.path
directory_exists = os.path.isdir(r'path of directory')
print(directory_exists)

Steps to Check if a File Exists using Python

Step 1: Capture the path where your file is stored

To start, capture the path where your file is supposed to be stored.

For example, a text file called ‘New_Products’ is stored under the following path:

C:\Users\Ron\Desktop\Test

How to Check if a File or Directory Exists using Python

Where the file name is ‘New_Products‘ and the file extension is txt.

Step 2: Check if the file exists using os.path.isfile

You can use the following template in order to check if a file exists:

import os.path
file_exists = os.path.isfile(r'path where the file is stored\file name.file extension')
print(file_exists)

For our example:

  • The path where the file is stored is: C:\Users\Ron\Desktop\Test
  • The file name is: New_Products
  • The file extension is: txt

Putting everything together (don’t forget to place the ‘r’ character before the path name to avoid errors with the path):

import os.path
file_exists = os.path.isfile(r'C:\Users\Ron\Desktop\Test\New_Products.txt')
print(file_exists)

As you can see, the value is ‘True‘ which means that the file exists in the path specified:

True value

Graphical User Interface to Check if a File Exists

Below is a graphical user interface (GUI) to check if a file exists.

The GUI is based on the tkinter package:

import tkinter as tk
import os.path

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 600, height = 300,  relief = 'raised')
canvas1.pack()

label1 = tk.Label(root, text='Check if a File Exists')
label1.config(font=('helvetica', 18))
canvas1.create_window(300, 35, window=label1)

label2 = tk.Label(root, text='Enter File Path:')
label2.config(font=('helvetica', 12))
canvas1.create_window(300, 100, window=label2)

entry1 = tk.Entry (root, width = 90) 
canvas1.create_window(300, 140, window=entry1)

def checkFile ():
     
    file_exists = os.path.isfile(entry1.get())
    
    label3 = tk.Label(root, text=str(file_exists),font=('helvetica', 14, 'bold'))
    canvas1.create_window(300, 250, window=label3)

button1 = tk.Button(text='File Exists?', command=checkFile, bg='brown', fg='white', font=('helvetica',12, 'bold'))
canvas1.create_window(300, 200, window=button1)

root.mainloop()

Run the code in Python, and you’ll get the following GUI:

How to Check if a File or Directory Exists using Python

Enter the full path in the entry box. Make sure to include the:

  • Path of the file
  • File name
  • File extension

For our example:

Check if a File or Directory Exists using Python

Once you click on the ‘File Exists?‘ button, you’ll get ‘True’ if the file actually exists (otherwise, you’ll get ‘False’).

Since the ‘New_Products’ file exists in the folder, the outcome is ‘True‘ as captured below:

tkinter GUI

Additional Resources

You can check the os.path module to read more about os.path.isfile.

You may also want to visit the tkinter documentation to learn more about graphical user interfaces.