Skip to Content

3 ways to fix FileNotFoundError: [Errno 2] No such file or directory

FileNotFoundError: [Errno 2] No such file or directory is an error that occurs when a Python program or script attempts to access a specific file but does not find the specified file in the designated location.

This generally indicates either that the path to the file is incorrect, or that the user does not have sufficient permissions to access it.

In this article, we will discuss 3 ways to fix this issue.

Check the spelling and case of the file or directory name

Make sure that you are spelling the file or directory name correctly and that you are using the correct case. There may be times when your filename will have been misspelled.

Get Your Free Linux training!

Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!

If you accidentally use escape sequences in a file path in Python, it can cause problems when trying to access the file.

For example, consider the following code:

with open('C:\path\to\nick\filename.ext', 'r') as f:
# Do something with the file
contents = f.read()
print(contents)

This code is intended to open the file filename.ext located at the path C:\path\to\nick\filename.ext.

However, the escape sequences \n are interpreted as a line break character. This may not be the intended path, and could cause the file to not be found.

To avoid this problem, you can use a raw string literal to specify the file path. A raw string literal is a string that is prefixed with the r character, which tells Python to interpret the string literally, without interpreting any escape sequences.

For example:

with open(r'C:\path\to\nick\filename.ext', 'r') as f:
# Do something with the file
contents = f.read()
print(contents)

This code will open the file filename.ext located at the path C:\path\to\nick\filename.ext, without interpreting the backslashes as escape sequences.

Check the path to the file or directory

Make sure that you are using the correct path to the file or directory.

It’s a common misconception that relative path is relative to the location of the python script, but this is not true. Relative file paths are always relative to the current working directory, and the current working directory doesn’t have to be the location of your python script .

You can use the ls command to list the contents of the directories along the path to the file or directory.

Using an absolute path to open a file in Python can be useful if you want to ensure that the file is always accessed from the same location, regardless of the current working directory.

file = open(r'C:\path\to\your\filename.ext') //absolute path

This code will open the file filename.ext located at the absolute path C:\path\to\your\filename.ext.

Change the current working directory before opening the file

You can use the os.chdir() function to change the current working directory before opening a file in Python.

For example:

import os
# Change the current working directory to 'C:\path\to\your\directory'
os.chdir(r'C:\path\to\your\directory')
# Open the file 'filename.ext' in the current working directory
with open('filename.ext', 'r') as f:
# Do something with the file
contents = f.read()
print(contents)

This code will change the current working directory to C:\path\to\your\directory, and then open the file filename.ext located in this directory.