File operations are frequently used in the programs. If we try to open a file whose path doesn’t exist, an error is thrown and the program halts. To avoid this, we can first check if the file exists in the given path and then open the file.
In today’s article, let’s discuss different ways to check if a file exists in Python without using the try-except/finally blocks. We will be using methods from the pathlib module and os modules.
Table of Contents
Method 1: Using pathlib.Path object
Python has an in-built module named pathlib that can be used to check for any file operations. Note that, this module is supported in Python versions 3.4+.
The Path object from the pathlib module supports various methods that can be used to check if a file or folder exists in the system.
- Use Path(“path-to-the-file-or-folder”).exists() to check if the file or folder exists.
- Use Path(“path-to-the-file-“).is_file() to check if the file exists in the given path.
- Use Path(“path-to-the-folder”).is_dir() to check if folder exists in the given path.
Note: We have to import the pathlib module to use the Path object.
Example 1:
Let’s say, we have a file named “demo.txt” in the current working directory. We can check if it exists as shown below:
from pathlib import Path if Path("demo.txt").exists(): print("File found")
Output:
File found
You can also check if the path exists and if it is a file using the below code:
from pathlib import Path if Path("demo.txt").is_file(): print("File found")
Output:
File found
Example 2:
Let’s say we have a directory named Solutions, we can check if it exists or not as shown below
from pathlib import Path if Path(r"C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions").exists(): print("Folder found")
Output:
Folder found
If you want to check if the specified path exists and if it is a folder, then use the is_dir() method as shown below.
from pathlib import Path if Path(r"C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions").is_dir(): print("Folder found")
Output:
Folder found
Method 2: Using os.path object
You can also use the os module and access the path object to determine if the file or folder exists in the given path string.
- Use os.path.exists(“path-to-the-file-or-folder”) to check if the file or folder exists.
- Use os.path.isfile(“path-to-the-file-“) to check if the file exists in the given path.
- Use os.path.isdir(“path-to-the-folder”) to check if folder exists in the given path.
Example 1:
Let’s say, we have a file named “demo.txt” in the current working directory. We can check if it exists as shown below:
import os if os.path.exists("demo.txt"): print("File found")
Output:
File found
If you specifically want to check if the path strings points to a file that exists, use the below snippet:
import os if os.path.isfile("demo.txt"): print("File found")
Output:
File found
Let’s say, we have a folder named Solutions as shown below. We can check if it exists using the below code snippet.
import os if os.path.exists(r"C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions"): print("Folder found")
Output:
Folder found
If you want to check if the path string is specifically a folder and it exists, use the isdir() method.
import os if os.path.isdir(r"C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions"): print("Folder found")
Output:
Folder found
Conclusion
This brings us to the end of this short tutorial. In this tutorial, we have discussed how to use two different modules to check the existence of a file and folder. We have also discussed different methods available within these modules to check the existence of a file, folder objects. If you see any error while opening the file or folder check this link FileNotFoundError: [Errno 2] No such file or directory
We hope this has been informative. Thank you for Reading.