FileNotFoundError: No such file or directory Error Python

featured Image

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:

  1. Absolute path, and
  2. Relative path

The absolute path starts from the root directory of your computer. For example, if you are a windows user, it would be C:Pythonnotesexample.txt.

So in code, we will write

my_file = open(r'C:Pythonnotesexample.txt')

lines = my_file.readlines()

The 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.

Using os module we can do two things:

  1. Check if the specified file exists in the current directory.
  2. 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 os.getcwd() function.

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 os.listdir() function.

my_files = os.listdir(current_dir)

The 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 in keyword.

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 os.chdir() function.

For example, let’s say our main python program is in the D:/Python_program directory and the file (example.txt) is found in C:/Python directory.

So to open the file, we will change the current directory to the path that contains the example.txt file.

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 os.chdir() function.

And then, we wrote the relative path of the file to open and read it.

Conclusion:

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:

Create Directory if not Exists in Python

Related Posts

merge json files in python

How to merge JSON files in python

JSON (JavaScript Object Notation) is a data format used for storing, transferring, and visualizing data. Its simple structure of key:value pairs and array data types makes JSON an easy and…

Read more
Install python pip in ubuntu

How to Install Pip(pip3) in Ubuntu Linux 22.04

Pip is a package manager for Python that allows you to install and manage additional Python packages that are not part of the Python standard library. Pip makes it easy…

Read more
featured Image

How to Write If-Else Statements in One Line in Python

If-else statements are a fundamental concept in Python and other programming languages. They allow you to execute different blocks of code based on whether a condition is true or false….

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *