How to Check if a File Exists in Python
Learn the simplest ways to check whether a file exists before reading, writing, renaming, or deleting it in Python.
Quick answer
from pathlib import Path
file_path = Path("data.txt")
if file_path.exists() and file_path.is_file():
print("File exists")
else:
print("File does not exist")
Use exists() to check whether the path is there, and is_file() to make sure it is a file, not a folder.
What this page helps you do
- Check whether a file is present before using it
- Avoid errors when opening or deleting files
- Understand the difference between a file path and a directory path
Best beginner option: pathlib
For new Python code, pathlib is usually the easiest option to read and understand.
Steps:
- Import
Pathfrompathlib - Create a
Pathobject with the file name or full path - Use
exists()to check whether the path exists - Use
is_file()when you specifically need a file
Example:
from pathlib import Path
file_path = Path("data.txt")
print(file_path.exists()) # True if the path exists
print(file_path.is_file()) # True if it is a file
print(file_path.is_dir()) # True if it is a folder
Check if a file exists
from pathlib import Path
file_path = Path("data.txt")
if file_path.exists() and file_path.is_file():
print("The file exists.")
else:
print("The file does not exist.")
Expected output if the file is present:
The file exists.
If you want to learn more about file paths, see working with file paths in Python.
Alternative option: os.path.exists()
You can also check file existence with os.path.exists().
This version:
- Works with string file paths
- Is common in older Python code
- Checks whether the path exists
- Does not confirm that the path is a file unless you also use
os.path.isfile()
Example:
import os
file_path = "data.txt"
if os.path.exists(file_path) and os.path.isfile(file_path):
print("File exists")
else:
print("File does not exist")
For new code, pathlib is usually clearer. If you want a function-by-function explanation, see os.path.exists() explained.
Check before opening a file
Checking first can be useful when you are reading a file that may not be there.
from pathlib import Path
file_path = Path("data.txt")
if file_path.exists() and file_path.is_file():
with file_path.open("r") as file:
content = file.read()
print(content)
else:
print("The file was not found.")
This can help beginners avoid confusion.
However, in real programs, try-except is often better because a file can still fail to open even if it exists. For example, it may be deleted or moved after your check.
If you are learning how to open files, see how to read a file in Python and the Python open() function explained.
Understand file vs directory
A path can point to either:
- A file
- A folder
This matters because exists() returns True for both.
Example:
from pathlib import Path
path = Path("my_folder")
print(path.exists()) # True if the folder exists
print(path.is_file()) # False for a folder
print(path.is_dir()) # True for a folder
Use:
exists()to check whether the path is thereis_file()when you specifically need a fileis_dir()when you specifically need a folder
When checking first is useful
Checking first is often helpful:
- Before reading a file
- Before renaming or deleting a file
- Before showing a custom message to the user
Example:
from pathlib import Path
file_path = Path("report.txt")
if file_path.exists() and file_path.is_file():
file_path.unlink()
print("File deleted.")
else:
print("File not found.")
When try-except may be better
Checking first is not always enough.
Problems can still happen because:
- Another program can change the file after you check
- The file may exist but still fail to open
- You may not have permission to access it
A safer pattern is to try the operation and handle errors if they happen.
from pathlib import Path
file_path = Path("data.txt")
try:
with file_path.open("r") as file:
content = file.read()
print(content)
except FileNotFoundError:
print("The file does not exist.")
except PermissionError:
print("You do not have permission to open this file.")
This approach is often better for real applications.
If you run into these problems, see FileNotFoundError in Python: causes and fixes and PermissionError in Python: causes and fixes.
Common mistakes
Here are some common reasons a file check does not work as expected:
- Using
exists()and assuming the path must be a file - Checking a relative path from the wrong working directory
- Misspelling the file name or extension
- Using backslashes incorrectly in Windows paths
- Expecting a file to exist before it has been created
Helpful debugging commands:
from pathlib import Path
print(Path("data.txt").resolve())
This shows the full path Python is using.
import os
print(os.getcwd())
This shows the current working directory.
from pathlib import Path
print(Path("data.txt").exists())
This checks whether the path exists.
from pathlib import Path
print(Path("data.txt").is_file())
This checks whether the path is a file.
from pathlib import Path
print(list(Path(".").iterdir()))
This lists the files and folders in the current directory.
Windows path tip
If you use backslashes in a Windows path, be careful. Some backslashes are treated as escape characters.
This can cause problems:
file_path = "C:\new\data.txt"
A safer option is to use a raw string or forward slashes:
file_path = r"C:\new\data.txt"
or:
file_path = "C:/new/data.txt"
FAQ
What is the simplest way to check if a file exists in Python?
Use pathlib.Path with exists() and is_file(). It is clear and beginner-friendly.
What is the difference between exists() and is_file()?
exists() checks whether the path exists. is_file() checks whether that path is a file.
Should I use os.path.exists() or pathlib?
For new code, pathlib is usually easier to read. os.path is still common in older code.
Can a file still fail to open after exists() returns True?
Yes. The file could be deleted, moved, or blocked after the check. try-except is safer for real programs.