Often Python applications need to work with one or more files as a part of their features. Sometimes, they need to create new files, modify existing files or even overwrite files. It is essential to check if a file exists or not before working with it. Otherwise, your application may request a non existent file and throw an error. In this article, we will learn how to check if file exists in Python.
Why Check If File Exists in Python
When your Python script or program accesses a file, it needs to be actually present and you need to have the adequate permissions to open it. Otherwise, Python will throw an Exception and your code will stop working. This will completely throw your application out of gear and stop all the processing its doing. Therefore, it is important to check if file exists in Python, before you can even start working with it.
Typically, Python developers wrap the code that accesses files, inside a try…except block. But it can be tedious to allow Python to throw an exception and then handle it elsewhere. A much cleaner way is to directly check if the file exists, before you even start processing it. Python provides many different functions for this purpose, that allow you to check if file or directory exists, without throwing an exception.
Different Ways to Check if File Exists in Python
There are many different ways to check if a file or directory exists in Python. We will look at the 4 most common solutions to this problem.
1. Using os.path.exists()
The OS library is a built-in module available with every Python installation. It provides many useful functions for Python interpreter to easily interact with OS and use its functions. Among them, os.path is a popular submodule that allows you to work with file paths and directories. It provides many different functions to check if file exists or not. You can use os.path.exists() function for this purpose. Here is its syntax.
os.path.exists(file_path)
In the above example, you can provide the absolute or relative path to your file. It returns true if the path exists, else it returns false.
Please note, if you provide relative path, then this function will look for the file relative to the directory in which this script is located.
Here is an example to demonstrate it.
>>> import os
>>> pth = '/home/ubuntu'
>>> os.path.exists(pth)
False
>>> pth = '/home/ubuntu/test.jpg'
>>> os.path.exists(pth)
True
In the first case, we provide a directory path and in the second case we provide a file path. So in the first case, it returns False and in the second case it returns True.
Please note, if you are running these commands on Windows, then you may need to escape the backslash in your file paths, to avoid being misinterpreted. Here is an example.
>>> import os
>>> pth = 'C:\test.jpg'
>>> os.path.exists(pth)
False
>>> pth = 'C:\\test.jpg'
>>> os.path.exists(pth)
True
Also, if you pass a symbolic link to file path that exists, then os.path.exists() will return True. Symbolic links are treated just as actual paths. Here is an example.
>>> pth = "/home/ubuntu"
>>> os.symlink(dirname, pth)
>>> os.path.exists(dirname)
False
>>> pth = "/home/ubuntu/test.jpg"
>>> os.symlink(dirname, pth)
>>> os.path.exists(dirname)
True
2. Using os.path.isfile()
You can also use os.path.isfile() function to check if a specified path points to a file or not. Here is its syntax.
os.path.isfile(file_path)
Here is an example.
>>> import os
>>> pth = '/home/ubuntu'
>>> os.path.isfile(pth)
False
>>> pth = '/home/ubuntu/test.jpg'
>>> os.path.isfile(pth)
True
Here also, you need to escape the backslashes in your file path, if you are using Windows. Also, symbolic links are treated exactly as actual paths to files and directories.
3. Using os.path.isdir()
On the other hand, if you want to check if a given path is a directory, you can use os.path.isdir() function. Here is its syntax.
os.path.isdir(directory_path)
Here is an example to illustrate it. It works opposite to what os.path.exists() and os.path.isfile(). It returns True if the path points to a directory, and False if it points to a file.
>>> import os
>>> pth = '/home/ubuntu'
>>> os.path.isfile(pth)
True
>>> pth = '/home/ubuntu/test.jpg'
>>> os.path.isfile(pth)
False
In this case also, you need to escape the backslash in your file paths, if you are using Windows. Also, symbolic links work the same way as actual paths. So if your directory exists, then the symbolic link to it will return True, else False.
4. Using pathlib.Path.exists()
Pathlib is another standard Python library that allows you to work with filesystem. It supports object oriented filesystem paths that are classified into pure paths that support only computation operations but not I/O operations, and concrete paths that support both computational and I/O operations.
It also provides pathlib.Path.exists() function that checks if a file path exists or not. If it exists, the function returns True, else it returns False.
>>> import pathlib
>>> path = '/home/ubuntu'
>>> pathlib.Path.exists(path)
False
>>> path = '/home/ubuntu/test.jpg'
>>> pathlib.Path.exists(path)
True
Conclusion
In this article, we have learnt several ways to check if file exists in Python. You can use any of them as per your requirement. Please ensure that the user that runs this code actually has the permission to access the file, otherwise you will get always get False. If you are using Windows, please remember to escape the backslashes in your file paths.
FAQs
1. Why Check If File Exists in Python
If you try to access a file that does not exist, in your Python script, then it will give an error and throw a FileNotFound Exception.
2. When to Check File existence?
It is best to check for file existence before you open the file to avoid any errors and exceptions.
3. Can I check multiple files at once?
The functions os.path.exists(), os.path.isfile(), os.path.isdir() and pathlib.Path.exists() all work on only 1 file at a time. If you want to check the existence of multiple files at once, you can loop through a list of file paths and call any of these functions in each iteration, on one file path at a time.
4. What are common mistakes while checking file existence?
Please make sure you provide the right path and the system user that runs the Python script has permission to access the file. Also, if you are providing file path on Windows, then you need to escape backslash by adding an extra backslash (\\). Also, remember that os.path.isdir() works only for directories and not files whereas the other 3 functions os.path.exists(), os.path.isfile() and pathlib.Path.exists() work for only directories.
Also read:
How to Merge Two Dictionaries in Python
How to Split List into Even Chunks in Python
How to Backup MySQL Database in Python
Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.