FileNotFoundError in Python — No such file Fix (2026) | Tutorials Logic
What is FileNotFoundError?
A FileNotFoundError occurs in Python when you try to open, read, or access a file that does not exist at the specified path. It is a subclass of OSError and was introduced in Python 3.3 as a more descriptive replacement for the generic IOError. This error is common when working with file paths, especially when the working directory or path separators differ between operating systems.
FileNotFoundError: [Errno 2] No such file or directory: 'data.txt'
Common Causes
Quick Fix (TL;DR)
# ❌ Problem
with open("data.txt") as f: # FileNotFoundError if file doesn't exist
content = f.read()
# ✅ Solution
import os
file_path = "data.txt"
if os.path.exists(file_path):
with open(file_path) as f:
content = f.read()
else:
print(f"File not found: {file_path}")
Common Scenarios & Solutions
Scenario 1: Wrong File Path
The most common cause is a simple typo in the file name or path. Python is case-sensitive on Linux/macOS, so Data.txt and data.txt are different files. Always verify the exact file name and path.
with open("Data.txt") as f: # FileNotFoundError (file is actually 'data.txt')
content = f.read()
with open("config.json") as f: # FileNotFoundError (file is 'config.JSON')
import os
# ✅ List files in directory to find exact name
print(os.listdir(".")) # Shows all files in current directory
# ✅ Use the correct case-sensitive name
with open("data.txt") as f:
content = f.read()
Scenario 2: File Deleted or Moved
If a file is deleted, moved, or renamed between when you check for it and when you open it, you get a FileNotFoundError. Use try/except to handle this gracefully in production code.
log_file = "/var/log/app.log"
with open(log_file) as f: # FileNotFoundError if log was rotated/deleted
data = f.read()
log_file = "/var/log/app.log"
try:
with open(log_file) as f:
data = f.read()
except FileNotFoundError:
print(f"Log file not found: {log_file}")
data = ""
except PermissionError:
print(f"No permission to read: {log_file}")
data = ""
Scenario 3: Relative vs Absolute Path
Relative paths are resolved from the current working directory, which may not be the directory containing your script. This is a common issue when running scripts from a different directory or in automated environments.
# Script is at /home/user/project/script.py
# data.txt is at /home/user/project/data.txt
# But script is run from /home/user/
with open("data.txt") as f: # FileNotFoundError — CWD is /home/user/, not /project/
import os
from pathlib import Path
# ✅ Build path relative to the script's location
script_dir = Path(__file__).parent
data_file = script_dir / "data.txt"
with open(data_file) as f:
content = f.read()
# ✅ Debug: print current working directory
print(f"CWD: {os.getcwd()}")
Scenario 4: Wrong Working Directory
When running scripts from an IDE, the working directory may be set to the project root rather than the script's directory. Use pathlib.Path(__file__).parent to always resolve paths relative to the script file itself.
# Works in terminal from project root, fails in IDE or CI
with open("src/config/settings.json") as f:
config = json.load(f) # FileNotFoundError in some environments
import json
from pathlib import Path
# ✅ Always resolve from the script's location
BASE_DIR = Path(__file__).resolve().parent.parent # project root
config_path = BASE_DIR / "src" / "config" / "settings.json"
with open(config_path) as f:
config = json.load(f)
Best Practices
Related Errors
- FileNotFoundError occurs when Python cannot find a file at the specified path.
-
File paths are case-sensitive on Linux and macOS —
Data.txtanddata.txtare different. - Relative paths are resolved from the current working directory, not the script's location.
-
Use
Path(__file__).parentto build paths relative to the script file for portability. - Always wrap file operations in try/except to handle FileNotFoundError gracefully.
-
Use
pathlib.Pathfor cross-platform path handling instead of string concatenation.
Frequently Asked Questions
Level Up Your Python Skills
Master Python with these hand-picked resources