FileNotFoundError: [Errno 2] No such file or directory

In Python, the files play a crucial rule while reading or writing the data in bulk. To do so, various methods, such as open(), write(), readline(), etc., are used. However, sometimes while accessing the file, the error “No such file or directory” is invoked in Python. To fix this error, different solutions are provided in this article in detail.

This write-up will provide the causes and solutions of the error “no such file or directory” in Python using the following terms:

Reason 1: File Does Not Exist

The “FileNotFoundError: [Errno 2] No such file or directory” appears in Python when a user tries to access a file that does not exist in the particular location, as shown below:

From the above snippet, it is verified that the program throws an error when we try to access a file that is not present in the directory.

Solution 1: Move the File to Python Working Directory

To fix this error, the file must be moved to a current working directory of Python, or the absolute path of the file must be written correctly.

File Location:

The file we want to access is currently placed in the current working directory of Python. The below snippet shows you the name and location of the file:

If you don’t know the current working directory of Python, you can use the given below code to get the current working directory:

Code:

import os

current_directory = os.getcwd()

print(current_directory)

In the above code, the “os.getcwd()” function of the standard “os” module is used to get the current working directory of Python.

Output:

The above output shows the present working directory of Python.

Now, after knowing the location of the file, we just simply type the name along with the format (.txt) to access the file in the Python program. An example code of this is shown below:

Code:

file = open("sample.txt", "r")

print(file.read())

In the above code, the “open()” function accepts the name of the file and mode of the file as an argument.

Output:

The above output shows that the program successfully read the file.

Solution 2: Provide Correct Absolute Path of File

Another way to solve the “no such file or directory” error in Python is by providing an absolute path inside the function.

File Location:

The path of the file placed at another location is shown below in the snippet:

Now, the above file is accessed using the “open()” function in the given below code example:

Code:

file = open("F:\itslinuxfoss\sample.txt", "r")

print(file.read())

In the above code, the absolute path along with the file name is placed inside the parentheses of “open()” function as a first argument. The mode “r” indicates that the file is accessed for reading purposes and is placed as the second argument.

Output:

The above output verified that the file had been successfully read using the specified location.

Reason 2: Incorrect File Name

The error also occurs due to incorrect file or path names as shown in the following snippet:

The above snippet shows that the program throws an error when the file name is misspelled.

Solution: Use the Correct File Name

To fix this error, correct the file name and always make sure to check the spelling of the file and the path location of the file carefully.

Code:

file = open("F:\itslinuxfoss\sample.txt", "r")

print(file.read())

In the above code, the file name is corrected and verified.

Output:

The above output verified that the file had been read successfully.

That’s it from this tutorial!

Conclusion

The “FileNotFoundError: [Errno 2] No such file or directory” error occurs when the user tries to access the file that does not exist at the specified location or at the current working directory of Python. To solve this error, move the file to the working directory of Python or provide a correct absolute path name. Several reasons and solutions for the error “No such file or directory” in Python are addressed in this article.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×