CK Logo
Sign in
Posts

Don’t lose your preferences!

Sign-in or sign-up to save your personalization preferences to return to later
Python FileNotFoundError

Python FileNotFoundError: [Errno 2] No such file or directory Solution

In most cases, any file you reference in a Python program needs to exist. This is, of course, unless you are creating a new file and writing to it. If you reference a file that does not exist, Python will return an error. One type of error is the FileNotFoundError, which is raised when referencing a file that does not exist using the os library.

What does the FileNotFoundError: [Errno 2] No such file or directory error mean?

The Python FileNotFoundError: [Errno 2] No such file or directory error is often raised by the os library. This error tells you that you are trying to access a file or folder that does not exist. To fix this error, check that you are referring to the right file or folder in your program.

Why am I seeing a FileNotFoundError: [Errno 2] No such file or directory error?

Any message with the contents FileNotFoundError indicates that Python cannot find the file you are referencing. Python raises this error because your program cannot continue running without being able to access the file to which your program refers.

This error is usually raised when you use the os library. You will see an IOError if you try to read or write to a file that does not exist using an open() statement.

How do I fix the Python FileNotFoundError Errno 2 No such file or directory?

To fix FileNotFoundError: [Errno 2] No such file or directory error, check that you are referring to the right file or folder in your program. Here is an example scenario featuring a FileNotFoundError message.

An Example Scenario

We’re writing a program that lists all the files in a folder. The folder we are referencing contains a list of markdown documentation for our project. To start, let’s import the os library, which has a method that lets us see all the files in a folder:

import os

Next, we are going to use the os.listdir() method to get a list of the files in our folder:
for f in os.listdir("/home/james/python_error/documentation/"):
print(f)

We retrieve a list of the files in the “/home/james/python_error/documentation/” folder. The for statement iterates over each file that the os.listdir() method finds. We print the name of each file to the console. Let’s see what happens when we run our code:

Our code returns:
FileNotFoundError: [Errno 2] No such file or directory:
'/home/james/python_error/documentation/'
Our code does not work.

The Solution

We have referenced a folder that does not exist. To solve the error in our program, we must make sure the directory to which we point exists. The actual folder with our docs is at /home/james/python_error/docs. Let’s change the folder to which our program refers to the one that actually contains our documentation:

import os

for f in os.listdir("/home/james/python_error/docs/"):
print(f)

Our code returns:
index.md

The output from our command is what we expected. We can see there is one file in our folder. If we had other files in the /home/james/python_error/docs/ folder, we would be able to see them in the output of our program.