Python FileNotFoundError: [Errno 2] No such file or directory Solution

Posted in /  

Python FileNotFoundError: [Errno 2] No such file or directory Solution

Vinay Khatri
Last updated on September 21, 2022

    When we read data from a file using Python there, we need to specify the file name. And that file needs to exist in the specified directory. If the file we are referencing in our program does not exist in the specified directory or folder, we will receive the FileNotFoundError: [Errno 2] No such file or directory Error.

    In this Python guide, we will walk through this error in detail and solve it. We will also discuss an example to demonstrate this error in Python. So let's get started with the error statement.

    Python Error: FileNotFoundError: [Errno 2] No such file or directory

    The error statement FileNotFoundError: [Errno 2] No such file or directory can be divided into two parts

    1. FileNotFoundErrro (Exception Type)
    2. [Error 2] No such file or directory

    1. FileNotFoundError

    This FileNotFoundError is one of the standard Python exceptions. It comes under the base exception of OSError. It is raised in a Python program when we try to access a file or directory that does not exist.

    2. [Errno 2] No such file or directory

    The [[Errno 2] No such file or directory] statement is the actual error message telling us that the file or directory we want to access in our Python program does not exist.

    Common Example Scenario

    Let's discuss this error statement with an example. In Python File Handling, we can read, write and append data between the files. But in the case of reading the data from a file, the file needs to be present in the specified directory. And while mentioning the file for reading, we also need to specify its full name, including the file extension. If we pass a file name that does not exist in the specified directory or even forget to specify the full name, we will receive the FileNotFoundError.

    Example (Error)

    Let's say we want to read data from our data.txt file, and while opening the file using the context manager with we do not mention the .txt extension, then see what happens to the program when we run it.

    # file name
    filename = 'data'
    
    # read the file
    with open(filename, 'r') as file:
        print(file.read())

    Output

    Traceback (most recent call last):
      File "main.py", line 5, in 
        with open(filename, 'r') as file:
    FileNotFoundError: [Errno 2] No such file or directory: 'data'

    Break the code

    In this example, we are getting the error in line 5 with " with open(filename, 'r') as file " statement. This error was raised because the Python open() function was not able to find any data file in the directory.

    Solution

    To solve the above problem, we need to make sure that the file we want to read mentions its full name. And in the above example, we are supposed to read the data.txt file not data .

    Example Solution

    # file name
    filename = 'data.txt'
    
    # read the file
    with open(filename, 'r') as file:
        print(file.read())

    Output

    Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean.
    A small river named D.......

    Wrapping Up!

    The Python FileNotFoundError: [Error 2] No such file or dirctory raised in a Python program when we try to access a file that does not exist in the system. You will mostly find this error in your Program when you deal with file handling and operating system file management. When you specify the file or directory name to any method like open(), you need to make sure that you are specifying the correct path and file name.

    If you are still getting this error in your python program, please share your code in the comment section. We will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments