- Python FileNotFoundError: [Errno 2] No such file or directory Solution
- Python FileNotFoundError: [Errno 2] No such file or directory
- Example FileNotFoundError
- Misspelled file name
- Invalid file path or directory path
- Using a relative path
- Solution to FileNotFoundError: [Errno 2] No such file or directory
- Python FileNotFoundError: [Errno 2] No such file or directory
- Error from open() function
- Error from os.listdir() function
- Conclusion
- Take your skills to the next level ⚡️
- About
- Search
Python FileNotFoundError: [Errno 2] No such file or directory Solution
In Python, when you reference a file, it needs to exist. Otherwise, Python will return a FileNotFoundError: [Errno 2] No such file or directory.
In this tutorial, let’s look at what is FileNotFoundError: [Errno 2] No such file or directory error means and how to solve this in your code.
Python FileNotFoundError: [Errno 2] No such file or directory
Python will raise FileNotFoundError when you use the OS library and try to read a file or write a file that does not exist using an open() statement.
It is, of course, excluding you are creating a new file and writing content to the file. Any error message which states FileNotFoundError means that Python cannot find the path of the file you are referencing.
Example FileNotFoundError
The below code will list all the files in a specified folder. We will be using the OS module and os.listdir() method to get a list of files in the specified folder.
import os for f in os.listdir("/etc"): print(f)
Traceback (most recent call last): File "Main.py", line 2, in for f in os.listdir("/etc/test"): FileNotFoundError: [Errno 2] No such file or directory: '/etc/test'
Now you can see that Python is throwing FileNotFoundError: [Errno 2] No such file or directory since the folder reference is wrong here.
The possible reasons for this error could be as follows.
Misspelled file name
The error will often occur due to misspelled filenames, so providing the correct file name would solve the issue.
Invalid file path or directory path
Sometimes you might give a wrong file path or directory path which does not exist. It usually happens even with the network path when it’s unreachable. So ensure that the file path is correct and if you are placing the file in the network path, make sure it’s reachable and accessible.
Using a relative path
If you use a relative path, the file would be searched in the current working directory and not in the original path. So ensure you give an absolute path of the file to resolve the error.
Solution to FileNotFoundError: [Errno 2] No such file or directory
We will correct our above code by referencing the proper directory where the file exists. This time, we will also use an absolute path instead of a relative path to ensure it’s referencing the correct directory.
import os for f in os.listdir("C:/Projects/Tryouts/etc"): print(f)
python.txt index.md Python Data Science Ebook.pdf
Python FileNotFoundError: [Errno 2] No such file or directory
This error can come from calling the open() function or the os module functions that deal with files and directories.
To fix this error, you need to specify the correct path to an existing file.
Let’s see real-world examples that trigger this error and how to fix them.
Error from open() function
Suppose you use Python to open and read a file named output.txt as follows:
Python will look for the output.txt file in the current directory where the code is executed.
If not found, then Python responds with the following error:
If the file exists but in a different directory, then you need to specify the correct path to that file.
Maybe the output.txt file exists in a subdirectory like this:
To access the file, you need to specify the directory as follows:
This way, Python will look for the output.txt file inside the logs folder.
Sometimes, you might also have a file located in a sibling directory of the script location as follows:
In this case, the script.py file is located inside the code folder, and the output.txt file is located inside the logs folder.
To access output.txt , you need to go up one level from as follows:
The path ../ tells Python to go up one level from the working directory (where the script is executed)
This way, Python will be able to access the logs directory, which is a sibling of the code directory.
Alternatively, you can also specify the absolute path to the file instead of a relative path.
The absolute path shows the full path to your file from the root directory of your system. It should look something like this:
Pass the absolute path and the file name as an argument to your open() function:
You can add a try/except statement to let Python create the file when it doesn’t exist.
The code above will try to open and read the output.txt file.
When the file is not found, it will create a new file with the same name and write some strings into it.
Also, make sure that the filename or the path is not misspelled or mistyped, as this is a common cause of this error as well.
Error from os.listdir() function
You can also have this error when using a function from the os module that deals with files and directories.
For example, the os.listdir() is used to get a list of all the files and directories in a given directory.
It takes one argument, which is the path of the directory you want to scan:
The above code tries to access the assets directory and retrieve the names of all files and directories in that directory.
If the assets directory doesn’t exist, Python responds with an error:
The solution for this error is the same. You need to specify the correct path to an existing directory.
If the directory exists on your system but empty, then Python won’t raise this error. It will return an empty list instead.
Also, make sure that your Python interpreter has the necessary permission to open the file. Otherwise, you will see a PermissionError message.
Conclusion
Python shows the FileNotFoundError: [Errno 2] No such file or directory message when it can’t find the file or directory you specified.
To solve this error, make sure you the file exists and the path is correct.
Alternatively, you can also use the try/except block to let Python handle the error without stopping the code execution.
Take your skills to the next level ⚡️
I’m sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I’ll send new stuff straight into your inbox!
About
Hello! This website is dedicated to help you learn tech and data science skills with its step-by-step, beginner-friendly tutorials.
Learn statistics, JavaScript and other programming languages using clear examples written for people.
Search
Type the keyword below and hit enter