FileNotFoundError: No such file or directory Error Python
📋 Table Of Content
While working with files or directories, we sometimes come across the "No such file or directory" Error in our python program.
The "FileNotFoundError: No such file or directory" error occurs when we try to access or open a file that doesn't exist at the specified location. To fix this error, we can add the file to the specified location or write the path to the program correctly.
Let's see an example of the error
my_file = open('example.txt', 'r') lines = my_file.readlines() print(lines)
And if we do not have the example.txt, then it will be unable to read the file and throw us the error in the terminal.
FileNotFoundError: [Errno 2] No such file or directory: 'example.txt'
Now to give the correct path of the file in the program we specify the path of the file. There are two types of paths in python:
- Absolute path, and
- Relative path
absolute path starts from the root directory of your computer. For example, if you are a windows user, it would be
So in code, we will write
my_file = open(r'C:\Python\notes\example.txt') lines = my_file.readlines()
relative path in python is the directory from which you are running the python code. It is also known as the working directory of your project.
So the relative path of the example.txt file in the working directory will be:
my_file = open('example.txt') lines = my_file.readlines()
Use os module in python
We can also use the
os module to print the content of the current working directory (cwd) and to check if the specified file exists or not.
os module we can do two things:
- Check if the specified file exists in the current directory.
- Change to the directory that contains the file.
Check if file exists in the current directory
To check the current directory of the program, we use the
import o current_dir = os.getcwd()
This will give the absolute path of the current directory of your program.
Next, to check the content of the directory we can use the
my_files = os.listdir(current_dir)
os.listdir() function gives us a list of files name in the working directory.
We can check for the specified file from the list using the
import os current_dir = os.getcwd() my_files = os.listdir(current_dir) print(example.txt in my_files) #False
It will print
False since the file is not present in the directory.
Change the current directory
We can use the
os module to change the current working directory to the specified path of the file. To change, we have to use the
For example, let's say our main python program is in the
D:/Python_program directory and the file (
example.txt) is found in
So to open the file, we will change the current directory to the path that contains the
import os current_file_dir = os.chdir(r'C:/Python') my_file = open('example.txt','r') lines = my_file.readlines() print(lines)
Here, since the example.txt file exists in
C:/Python folder, we have changed the current directory using
And then, we wrote the relative path of the file to open and read it.
Here, we have learned to solve the "FileNotFoundError: [Errno 2] No such file or directory" error from our python program. And learned to set the absolute and relative path of our files using os module.
Other Articles You'll Also Like: