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.
In this guide, we’re going to walk you through what the
FileNotFoundError: [Errno 2] No such file or directory error means and how you can solve it in your code. Without further ado, let’s begin.
Python FileNotFoundError: [Errno 2] No such file or directory
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.
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
Let’s take a look at 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:
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.
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:
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.
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.
Now you have the knowledge you need to successfully fix this Python error.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.