How to Check if a File Exists in Python (With Easy Examples)
Check if File Exists in Python
๐ Key Highlights
- โจ Learn 7 easy methods to check if a file exists in Python.
- โจ Use practical code examples to understand each method.
- โจ Avoid errors and data loss by verifying file existence before operations.
- โจ Bonus: Discover when to use each method depending on your Python version.
- โจ Includes internal links to other Python file handling tutorials.
๐ฅ Why You Must Know How to Python Check if File Exists?
Imagine working on a big project where you read or write files often. What if the file you want to open doesnโt exist? Your program will crash, causing wasted time and frustration.
Table Of Content
- Check if File Exists in Python
- ๐ Key Highlights
- ๐ฅ Why You Must Know How to Python Check if File Exists?
- โ 1. Using os.path.exists() to Check if File Exists
- โ Explanation
- โ 2. Python Check if File Exists and Is a File Using os.path.isfile()
- โ 3. Using pathlib.Path.is_file() for Modern Python (3.4+)
- โ Benefits of pathlib
- โ 4. Check If File Exists Using try-except Block
- โ 5. Check If File Exists and Is Readable Using os.access()
- โ 6. Combining os and pathlib for Cross-Version Compatibility
- โ 7. Using Glob Patterns to Check If Files Matching a Pattern Exist
- ๐ฏ Best Practices When You Python Check if File Exists
- ๐ Related Python File Handling Tutorials
- ๐ Final Thoughts: Master Python Check if File Exists Like a Pro!
- ๐ FAQs on Python Check if File Exists
- โ How do I check if a file exists in Python?
- โ How do I check if a file exists without opening it?
- โ Can I check if a file exists and is readable?
- โ Share this guide if it helped you and bookmark it for later! ๐
That’s why it’s critical to check if file exists before proceeding.
โก๏ธ Python check if file exists helps to:
- Avoid overwriting important data.
- Confirm that required files are in place.
- Prevent runtime errors.

If you’ve ever been stuck wondering how to check if a file exists, this guide will give you all the answers! Let’s dive in! ๐
โ
1. Using os.path.exists() to Check if File Exists
import os file_path = "path/to/your/file.txt" if os.path.exists(file_path): print("โ
File exists.") else: print("โ File does not exist.")
โ Explanation:
os.path.exists()checks if a file or directory exists.- Returns
Trueif the file exists; otherwise,False.
๐ก Tip: You can use this to check directories too!
โ
2. Python Check if File Exists and Is a File Using os.path.isfile()
import os file_path = "path/to/your/file.txt" if os.path.isfile(file_path): print("โ
It's a file and it exists!") else: print("โ File does not exist.")
๐ Note: This is more specific than os.path.exists().
โ
3. Using pathlib.Path.is_file() for Modern Python (3.4+)
from pathlib import Path file_path = Path("path/to/your/file.txt") if file_path.is_file(): print("โ
File exists.") else: print("โ File does not exist.")
โ Benefits of pathlib:
- More readable and modern code.
- Better path manipulations.
๐ Recommended if you use Python 3.4 or later!
โ
4. Check If File Exists Using try-except Block
file_path = "path/to/your/file.txt" try: with open(file_path, 'r') as file: print("โ
File exists and opened successfully.") except FileNotFoundError: print("โ File does not exist.")
๐ก Bonus: You can read the file directly if it exists!
โ
5. Check If File Exists and Is Readable Using os.access()
import os file_path = "path/to/your/file.txt" if os.access(file_path, os.R_OK): print("โ
File exists and is readable.") else: print("โ File does not exist or not readable.")
๐ Tip: Replace os.R_OK with os.W_OK to check for write permission.
โ
6. Combining os and pathlib for Cross-Version Compatibility
import os from pathlib import Path file_path = "path/to/your/file.txt" if os.path.exists(file_path) and Path(file_path).is_file(): print("โ
File exists.") else: print("โ File does not exist.")
โ 7. Using Glob Patterns to Check If Files Matching a Pattern Exist
import glob files = glob.glob("path/to/your/*.txt") if files: print(f"โ
Found {len(files)} file(s).") else: print("โ No matching files found.")
๐ Use Case: Find all .txt files in a folder.

๐ฏ Best Practices When You Python Check if File Exists
- โ Always check before reading/writing to prevent errors.
- โ Use pathlib for modern Python code.
- โ
Handle exceptions properly (e.g.,
FileNotFoundError). - โ
When working with large directories, prefer
globfor pattern matches.
๐ Related Python File Handling Tutorials
๐ Final Thoughts: Master Python Check if File Exists Like a Pro!
By now, youโve learned 7 powerful ways to Python check if file exists. Whether you prefer os, pathlib, or try-except, these methods will help you write error-free and efficient code.
โ๏ธ Mastering the check if file exists technique will make your Python projects more reliable and professional.
๐ FAQs on Python Check if File Exists
โ How do I check if a file exists in Python?
Use os.path.exists('filename') or pathlib.Path('filename').is_file() for a quick check.
โ How do I check if a file exists without opening it?
Use os.path.exists() or pathlib.Path().is_file() without opening the file.
โ Can I check if a file exists and is readable?
Yes! Use os.access('filename', os.R_OK) to check for read permissions.


Really helpful guide! I think itโs also important to note that `os.path.exists()` can return `True` for directories as well. I tend to get confused about that sometimes.