Python provides several ways to check if a file or directory exists. In this article, we will go over the most commonly used methods to check if a file exists in Python.

The first method is to use the os.path.exists() function. This function takes a single argument, which is the path to the file or directory you want to check for. It returns True if the file or directory exists, and False otherwise. For example: 

				
					import os
if os.path.exists("example.txt"):
    print("File exists")
else:
    print("File does not exist")

				
			

Another method is to use the os.path.isfile() function. This function also takes a single argument, which is the path to the file or directory you want to check for. It returns True if the path points to a file, and False otherwise. For example:

				
					import os
if os.path.isfile("example.txt"):
    print("Path is a file")
else:
    print("Path is not a file")

				
			

You can also use the os.path.isdir() function to check if a path points to a directory. This function also takes a single argument, which is the path to the file or directory you want to check for. It returns True if the path points to a directory, and False otherwise. For example:

				
					import os
if os.path.isdir("example"):
    print("Path is a directory")
else:
    print("Path is not a directory")

				
			

Another way to check if a file exists is to try to open it. If the file does not exist, the open() function will raise a FileNotFoundError exception. You can catch this exception and use it to determine if the file exists or not. For example:

				
					try:
    with open("example.txt") as f:
        print("File exists")
except FileNotFoundError:
    print("File does not exist")

				
			

You can also use the os.access() function to check if a file exists and is accessible. This function takes two arguments: the first is the path to the file, and the second is an integer value that represents the access mode. The most commonly used value for the access mode is os.F_OK, which checks if the file exists. For example:

				
					import os
if os.access("example.txt", os.F_OK):
    print("File exists")
else:
    print("File does not exist")

				
			

In addition to the methods mentioned above, there is also the os.stat() function, which returns information about a file or directory. The returned information includes the file’s size, access and modification times, and more. If the file does not exist, the os.stat() function will raise an OSError exception. This can be used to check if the file exists or not. For example:

				
					import os
try:
    os.stat("example.txt")
    print("File exists")
except OSError:
    print("File does not exist")

				
			

Another method is to use the pathlib module, which provides an object-oriented interface for working with file paths. The Path class in the pathlib module provides a exists() method that can be used to check if a file or directory exists. For example:

				
					from pathlib import Path

if Path("example.txt").exists():
    print("File exists")
else:
    print("File does not exist")

				
			

It’s also worth noting that when checking for the existence of a file, it is important to consider the current working directory. The current working directory is the directory where the Python script is running from, and it may not be the same directory where the file you want to check for exists. To ensure that your script is looking for the file in the correct directory, you can use the os.path.abspath() function to get the absolute path of the file.

In addition, when checking for the existence of a file in a directory, it is also important to consider permissions. The file or directory may exist, but the user may not have the necessary permissions to access it.

Wrap up

Checking if a file exists in Python can be done using a variety of built-in functions and modules such as os.path.exists(), os.path.isfile(), os.path.isdir(), open(), os.access(), os.stat(), pathlib.Path.exists() etc. The choice of which method to use depends on the specific requirements of the task at hand, and it’s important to consider current working directory and permissions when working with files and directories.


Thanks for reading. Happy coding!