The best modern way to check if a file exists in Python is Path(path).is_file() from pathlib. It returns True only when the path exists and is a regular file. Use Path.exists() when you want to know whether any filesystem object exists, including a file, directory, or symlink target.
For new code, prefer pathlib because it gives you readable path objects and methods such as exists(), is_file(), and is_dir(). The older os.path functions still work and are useful in legacy code. The official pathlib Path.exists documentation and os.path.exists documentation cover both styles.
The main decision is whether you need any path or specifically a file. That distinction matters because opening a directory as a file raises an error. Make the check match the operation you plan to perform next.
Check if a Path Is a File with pathlib
Use is_file() when your code specifically needs a file, not a directory. This avoids treating a folder as if it can be opened like a regular file.
from pathlib import Path
path = Path("data.csv")
if path.is_file():
print("File exists")
else:
print("File is missing or not a regular file")
This is usually the right check before reading a known file path. It is clearer than exists() when directories should not count as a success. See the official Path.is_file documentation for the exact behavior.
Check if Any Path Exists
Use exists() when a directory, file, or other path type should count as present. This is common before creating a directory tree or checking whether a configured path is available.
from pathlib import Path
path = Path("reports")
if path.exists():
print("Path exists")
else:
print("Path does not exist")
If you need to know what kind of path exists, follow up with is_file() or is_dir(). A plain existence check can be too broad for file-reading code. It can also hide a configuration problem if a directory exists where a file was expected.
Check if a Path Is a Directory
Use is_dir() when the path should be a folder. This is different from checking whether a file exists, and it is the right guard before listing directory contents.
from pathlib import Path
folder = Path("reports")
if folder.is_dir():
print("Directory exists")
else:
print("Directory is missing")
The official Path.is_dir documentation explains the directory check. If you plan to list files inside a folder, the Python os.listdir guide covers related directory listing patterns.
Use os.path.exists in Older Code
Legacy projects often use os.path.exists() and os.path.isfile(). They are still valid, especially when a codebase already uses string paths everywhere.
import os
path = "data.csv"
if os.path.isfile(path):
print("File exists")
else:
print("File is missing")
For new code, pathlib is usually more ergonomic. For older code, consistency can matter more than switching every path operation at once. If you migrate to pathlib, do it around a clear boundary such as a function or module instead of mixing styles on every line.
Use try/except When You Will Open the File
Checking first is not always enough. A file can be deleted or moved between the check and the open operation. If the next step is opening the file, handle FileNotFoundError around the open call.
from pathlib import Path
path = Path("data.csv")
try:
text = path.read_text(encoding="utf-8")
except FileNotFoundError:
print("File was not found")
else:
print(text[:50])
This pattern is safer for production code because it handles the actual operation that can fail. The official FileNotFoundError documentation covers the exception raised for missing files. You may also need to handle PermissionError if the file exists but cannot be read.
Check Before Getting File Size
Once you know a path is a file, you can safely read metadata such as file size. With pathlib, use stat().
from pathlib import Path
path = Path("data.csv")
if path.is_file():
size = path.stat().st_size
print(size)
else:
print("No file size available")
For more file metadata examples, see the Python get file size guide. If your path comes from user input or stdin, validate it before using it; the read input from stdin guide covers that related workflow.
Best Practice
Use Path(path).is_file() when you specifically need a file. Use Path(path).exists() when any path type is acceptable. Use Path(path).is_dir() for directories. If you will immediately open the file, use try/except FileNotFoundError around the file operation. If you are checking runtime state rather than files, the check if variable exists guide covers a separate Python problem.
Keep paths as Path objects as long as possible and convert to strings only when a third-party API requires it. That keeps path operations readable and reduces small bugs caused by manual string concatenation. For symlinks and shared folders, remember that existence can change between checks, so exception handling around the real file operation is still the final safety net.