Python: “can’t open file” Error – Causes and Fixes
If you’ve been working with Python and suddenly see an error like:
python: can't open file 'script.py': [Errno 2] No such file or directoryyou’re not alone. The “can’t open file” error is one of the most common issues Python beginners (and even experienced developers) encounter. While it may look intimidating, the cause is usually straightforward — and so is the fix.
In this guide, we’ll break down why this error happens, the different variations you might see, and step-by-step solutions to get your code running again.
Understanding the “can’t open file” Error
The error appears when Python cannot locate or access the file you’re trying to run.
The most common format looks like this:
python: can't open file 'myfile.py': [Errno 2] No such file or directoryBut you might also see variations such as:
python3: can't open file 'main.py': [Errno 13] Permission deniedpython: can't open file 'program.py': [Errno 2] No such file or directorypython: can't open file '/path/to/file.py': [Errno 2] No such file or directory
In most cases, it’s caused by one of the following issues:
- The file doesn’t exist in the location you’re running the command.
- You’re in the wrong directory when running Python.
- You don’t have permission to access the file.
- The file name is misspelled or has the wrong extension.
Common Causes and How to Fix Them
1. You’re in the Wrong Directory
When running Python scripts from the terminal, you need to be in the correct folder where the file exists.
Example Problem:
You’re in the Downloads folder but your Python file is in Documents.
How to Fix:
- Navigate to the folder containing your script:
cd /path/to/your/file - Run the script again:
python script.py
Tip: Use ls (Mac/Linux) or dir (Windows) to list files in the current directory and confirm the file is there.
2. The File Doesn’t Exist
If the file you’re trying to run simply doesn’t exist, Python can’t open it.
Example Problem:
python my_script.py
# Error: can't open file 'my_script.py'How to Fix:
- Double-check that you actually created and saved the file.
- Confirm the spelling — file names are case-sensitive on Mac/Linux.
- Make sure you saved the file with the .py extension.
3. Permission Issues
Sometimes the error occurs because you don’t have permission to read or execute the file.
Example Problem:
python3 main.py
# Error: Permission deniedHow to Fix (Mac/Linux):
Give yourself permission to read the file:
chmod +r main.pyIf you need to execute it directly:
chmod +x main.pyOn Windows:
- Right-click the file → Properties → Security tab → grant read/execute permissions to your user.
4. Wrong Python Command
You might be running the wrong Python version or interpreter.
Example Problem:
- Using
pythonwhen you should be usingpython3(common on macOS/Linux). - Running
py script.pyon Windows withoutpyinstalled.
How to Fix:
Check your Python version:
python --version
python3 --versionThen use the correct one for your environment:
python3 script.pyor
py script.py5. Relative vs. Absolute Path Issues
If you’re running a script from another folder, you need to specify the full path.
Example:
Instead of:
python my_script.pyUse:
python /Users/username/Documents/my_script.pyTip: On Windows, remember to use quotes if your path has spaces:
python "C:\Users\Name\My Documents\my_script.py"6. File is Locked or In Use by Another Program
On Windows especially, files can be locked by other applications.
How to Fix:
- Close any program that might have the file open.
- Save your changes, then try running the script again.
Extra Troubleshooting Tips
- Check File Encoding: If the file was created on another operating system, line endings or encoding may cause issues. Open it in a plain text editor like VS Code or Notepad++.
- Avoid Special Characters: Use only letters, numbers, underscores, and hyphens in file names.
- Keep Paths Short: Very long directory paths can cause problems, especially on Windows.
Example: Fixing the Error Step-by-Step
Let’s say you tried:
python app.pyand got:
python: can't open file 'app.py': [Errno 2] No such file or directoryHere’s a quick fix process:
- Check Current Directory:
pwd # Mac/Linux cd # Windows PowerShell - List Files:
ls # Mac/Linux dir # Windows - Navigate to Correct Folder:
cd path/to/your/script - Run Again:
python app.py
If it still doesn’t work, check spelling, file permissions, and whether the file even exists.
Preventing the “can’t open file” Error in the Future
- Organize Your Projects: Keep each project in its own folder.
- Use an IDE: VS Code, PyCharm, or Thonny can automatically set the working directory.
- Learn Relative Paths: Knowing how to navigate folders in the terminal can save you hours of frustration.
- Version Control: Using Git ensures you don’t lose files accidentally.
Conclusion
The Python “can’t open file” error might look serious, but it’s usually caused by something simple like a missing file, wrong path, or permission issue. By checking your current directory, confirming the file’s existence, and ensuring you have the right permissions, you can fix it quickly and get back to coding.
Understanding how file paths and directories work in Python will also make you more confident and efficient as a developer.

