You tried to open a file in Python and got “FileNotFoundError: No such file or directory”?
This error means Python can’t find the file at the path (location) you specified. It could be a typo in the file name, or the file might be in a different folder than Python is looking in.
Don’t worry, this is a very common issue. Once you understand how file paths work, you’ll fix it in no time.
目次
Why FileNotFoundError happens in Python
Python raises FileNotFoundError when it can’t locate a file. The usual causes are:
- Wrong file name or typo: The file name in your code doesn’t match the actual file name (including the extension)
- Wrong directory: Python is looking in a different folder than where the file actually is
- The file doesn’t exist: The file hasn’t been created yet, or was moved or deleted
Solution 1: Check the file name and path (most common fix)
Most FileNotFoundErrors come from a simple mismatch between the path in your code and the actual file location.
1. Read the error message carefully
# Example error
FileNotFoundError: [Errno 2] No such file or directory: 'data.csv'
# → Python is looking for "data.csv" but can't find itThe file name at the end of the error is exactly what Python searched for.
2. Double-check the file name, including the extension
# ✗ Common mistakes
open("data.csv") # File is actually named "Data.csv" (capital D)
open("data.csv") # File is actually "data.CSV" (uppercase extension)
open("data") # Missing the .csv extension
# ✓ Make sure the name matches exactly
open("Data.csv")File names are case-sensitive on Mac and Linux (though not on Windows). Always match the exact name.
3. Check where Python is running from
# Add this to your code to see Python's current working directory
import os
print(os.getcwd())If the file is in a different folder than what os.getcwd() shows, that’s the problem. See Solution 2 for how to fix it.
Solution 2: Use the correct file path
If the file exists but is in a different folder, you need to tell Python the full path.
1. Use an absolute path (the full address)
On Windows:
# Use the full path with raw string (r"...") to avoid backslash issues
file = open(r"C:\Users\YourName\Documents\data.csv")
On Mac:
# Mac uses forward slashes
file = open("/Users/YourName/Documents/data.csv")
If the file opens without errors, you’ve got the right path.
2. Or use a path relative to your script’s location
import os
# Get the directory where your script is located
script_dir = os.path.dirname(os.path.abspath(__file__))
# Build the path to the file relative to your script
file_path = os.path.join(script_dir, "data.csv")
file = open(file_path)
This approach works regardless of where you run the script from, because it always starts from the script’s own folder.
3. Verify the file exists before opening
import os
file_path = "data.csv"
if os.path.exists(file_path):
file = open(file_path)
print("File opened successfully")
else:
print(f"File not found: {os.path.abspath(file_path)}")
# This prints the full path Python is looking at
The os.path.abspath() output shows you exactly where Python is looking. Compare that with the file’s actual location.
Solution 3: Fix common path mistakes on Windows
Windows file paths have a few quirks that trip up beginners.
1. Backslash problems
# ✗ Backslashes are treated as escape characters
open("C:\Users\new_file\data.csv") # \n and \d are escape sequences!
# ✓ Option A: Use raw strings (prefix with r)
open(r"C:\Users\new_file\data.csv")
# ✓ Option B: Use forward slashes (works on Windows too)
open("C:/Users/new_file/data.csv")
# ✓ Option C: Use double backslashes
open("C:\\Users\\new_file\\data.csv")2. Spaces in file paths
# Spaces in folder names are fine — just include them in the string
open(r"C:\Users\Your Name\My Documents\data.csv")As long as the path is inside quotes, spaces work normally.
If nothing works
If you still can’t get the file to open:
- List all files in the directory: This confirms what files Python can actually see
import os
# List all files in the current directory
for filename in os.listdir("."):
print(filename)- Check file permissions: On Mac or Linux, the file might exist but Python might not have permission to read it. Use
ls -lain the terminal to check - Tip for asking for help: Include the full error message, the path you’re using, and the output of
os.getcwd(). This helps others pinpoint the issue
Summary
- FileNotFoundError means the file path in your code doesn’t match the actual file location — check for typos, including the file extension
- Use
os.getcwd()to see where Python is looking, and use absolute paths oros.path.join()for reliable paths - On Windows, use raw strings (
r"...") or forward slashes to avoid backslash escape issues
Related articles:
- name-error-not-defined.html (NameError: name is not defined — how to fix)
- type-error-python.html (How to fix TypeError in Python)
- syntax-error-invalid-syntax.html (SyntaxError: invalid syntax — causes and fixes)

















Leave a Reply