Skip to content

Python: “can’t open file” Error – Causes and Fixes

5 min read

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 directory

you’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 directory

But you might also see variations such as:

  • python3: can't open file 'main.py': [Errno 13] Permission denied
  • python: can't open file 'program.py': [Errno 2] No such file or directory
  • python: 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:

  1. The file doesn’t exist in the location you’re running the command.
  2. You’re in the wrong directory when running Python.
  3. You don’t have permission to access the file.
  4. 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:

  1. Navigate to the folder containing your script: cd /path/to/your/file
  2. 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 denied

How to Fix (Mac/Linux):
Give yourself permission to read the file:

chmod +r main.py

If you need to execute it directly:

chmod +x main.py

On Windows:

  • Right-click the file → PropertiesSecurity 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 python when you should be using python3 (common on macOS/Linux).
  • Running py script.py on Windows without py installed.

How to Fix:
Check your Python version:

python --version
python3 --version

Then use the correct one for your environment:

python3 script.py

or

py script.py

5. 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.py

Use:

python /Users/username/Documents/my_script.py

Tip: 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.py

and got:

python: can't open file 'app.py': [Errno 2] No such file or directory

Here’s a quick fix process:

  1. Check Current Directory: pwd # Mac/Linux cd # Windows PowerShell
  2. List Files: ls # Mac/Linux dir # Windows
  3. Navigate to Correct Folder: cd path/to/your/script
  4. 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.