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

Posted in /  

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

Vinay Khatri
Last updated on April 18, 2024

    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 reference in our program does not exist in the specified directory or folder, we receive the FileNotFoundError: [Errno 2] No such file or directory Error.

    This Python guide will thoroughly walk you through this error and help you 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 has 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 occurs 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

    When we want to read data from a file in Python, the file needs to be present in the specified directory. We 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 receive the FileNotFoundError.

    Example

    Let's say we want to read data from the data.txt file. While opening the file using the context manager with , we do not mention the .txt extension. 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 received the error in line 5 as " with open(filename, 'r') as file " statement. We received it because the Python open() function could not find any data file in the directory.

    Solution

    To solve the above problem, we must ensure that we have mentioned the file's 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.......

    Some Other Reasons

    Besides the file not existing in the directory or system, there are a few other reasons for the FileNotFoundError: [Error 2] No such file or dirctory error to occur. They are as follows:

    1. Misspelled Filename

    One of the reasons for this error to occur is the misspelled filename. It might be a case that you enter a filename with a typo. Hence, it is always helpful to recheck the entered filename.

    2. Using Escape Sequences in a File Path Accidentally

    Consider a path.

    path = 'C:\Users\neo\filename.ext'

    Here, the path contains a line break character, '\n' in 'Users\neo'.

    Though it is not an error or wrong path name, the Python interpreter considers '\n' as a line break character. The only solution to this problem is to use raw string literals for file paths.

    path = r'C:\Users\neo\filename.ext'

    Wrapping Up!

    This was all about the FileNotFoundError: [Error 2] No such file or dirctory error. We get this error when we try to access a file that does not exist in a directory.

    We will mostly encounter this error when we deal with file handling and operating system file management. When we specify the file or directory name to any method like open(), we need to specify the correct path and file name.

    If you still get this error in your Python program, please share your code in the comment section. We will try to help you with debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments