3 Easy Ways to Check If File Exists in Python

In Python, numerous operations are performed on the file using different modules and functions. However, before performing any operation, such as reading, writing, appending, etc., it must be verified that the selected file exists. To check whether the file is present or not the built-in functions like “os.path.exists()”, “os.path.isfile()”, etc. are used in Python.

In this blog, we will explain different methods to check if a file exists or not at the specified location/path. 

Sample File

Our sample file is located at the following path:

Method 1: Using os.path.exists() Function

The below code uses the “os.path.exists()” function to check if the file exists at the given location:

Code:

import os
file_location = 'C:/Users/p/Downloads/ILf/sample.txt'
if os.path.exists(file_location):
    print('File exists!')
else:
    print('File does not exist.')
  • In the beginning of the program, the “os” module is imported.
  • The file’s name along with the complete path is assigned to a variable named “file_location”.
  • The “if” statement is used along with the “os.path.exists()” function to check whether the file exists at the specific location or not.
  • If the file exists at the provided path then the “File exists!” message appears on the screen.
  • If the file does not exist then the else block returns the “File does not exist” message on the screen.

Output:

The selected file is present at the given path.

Method 2: Using os.path.isfile() Function

The “os.path.isfile()” function is used to find the specific file at the particular location:

Code:

import os
file_location = 'C:/Users/p/Downloads/ILf/sample.txt'
if os.path.isfile(file_location):
    print('File exists!')
else:
    print('File does not exist.')

The “os.path.isfile()” function is used along with the “if-else” statement to check whether the file exists or not. The stated function takes the complete path along with the name of the file. 

Output:

The output states that the selected file exists at the given location.

Method 3: Using pathlibPath.exists() Function

The “pathlib” module provides a function named “pathlibPath.exists()” that checks the file existence at a specific location:

Code:

import pathlib
file_path = pathlib.Path("C:/Users/p/Downloads/ILf/sample.txt")
if file_path.exists ():
    print ("file exist!")
else:
    print ("file not exist")
  • The complete path of the file is set to a variable named “file_path”.
  • The “if” statement is used along with the “file_path.exists()” function to check whether the file is present at the given location or not.

Output:

The stated file is located at the given location.

How to Check the Existence of a Directory?

Python allows us to check the directory’s existence using different built-in functions. For instance, the “os.path.isdir()” function is used in Python to check if a directory exists at the given path or not. Here is an example:

Code:

import os
file_location = 'C:/Users/p/Downloads/ILf'
if os.path.isdir(file_location):
    print('Directory exists!')
else:
    print('Directory does not exist.')
  • The directory’ complete path is assigned to a variable named “file_location”.
  • The “os.path.isdir()” function is passed as the condition of the “if” statement. This function accepts the file location and checks whether the directory exists at the given location or not.
  • The if block executes its statement when the directory exists and the else block executes when the directory is not present.

Output:

The given directory is found at the given location.

Conclusion

To check if a file exists at the given path, the “os.path.exists()” function, “os.path.isfile()” function, and “pathlibPath.exists()” function is used in Python. The “os.path.exists()” and “os.path.isfile()” functions accept the file’s location along with the name to check whether the file is present at the specified location or not. We can also check the existence of a directory using the “os.path.isdir()” function in Python. This guide provided a precise guide on how to check if the file exists using numerous examples.