Python

[Python] FileNotFoundError reason and solution

Share this for your friends.

[Python] FileNotFoundError reason and solution

When Python code reads file, it may cause FileNotFoundError.
The reason of the error is "File is not found".
If it reads single file, it can be OK to prepare the missing file.
But in case of multiple files, you would like to continue the process.

In this article, I introduce the reason and solution about FileNotFoundError.


Cause of FileNotFoundError

The cause of FileNotFoundError is "File is not found".
It means that it tried to access particular files but the files does not exist.

In the example below, it tries to read from 0.csv to 2.csv. But 1.csv does not exist. So it causes FileNotFoundError.

file_not_found_v0.py
csv
└0.csv
└2.csv
import pandas as pd

for i in range(3):
    filename = "csv/{}.csv".format(str(i))
    print(filename)
    df = pd.read_csv(filename)
    print(df)
    print()

# csv/0.csv
#   column
# 0  value
# 1  value
# 2  value

# csv/1.csv
# FileNotFoundError: [Errno 2] File b'csv/1.csv' does not exist: b'csv/1.csv'


How to avoid FileNotFoundError

Like the previous example, sometimes there is a lack of files.

Sometimes you would like to stop the process.
Sometimes you want to run the code with existing files.

In order to avoid FileNotFoundError, there are 2 ways.

Avoiding FileNotFoundError

  • Check existence
  • Skip the error

  • Check existence

    The FileNotFoundError happens when we try to access non-existent file.
    So we can avoid it by checking existence.

    In order o check existence, we can use os.path.exists().
    You can set file path as a parameter.

    import pandas as pd
    import os
    
    for i in range(3):
        filename = "csv/{}.csv".format(str(i))
        print(filename)
    
        if os.path.exists(filename):
            df = pd.read_csv(filename)
            print(df)
        else:
            print("{} not found.".format(filename))
        print()
    
    # csv/0.csv
    #   column
    # 0  value
    # 1  value
    # 2  value
    #
    # csv/1.csv
    # csv/1.csv not found.
    #
    # csv/2.csv
    #   column
    # 0  value
    # 1  value
    # 2  value
    


    Skip the error

    Another method is skipping the error.
    Even FileNotFoundError occurs, run the process.
    In order to do it, use try and except.
    Once FileNotFoundError occurs, go to except process andrun the next loop process.

    Then you can run the process with existing files.

    for i in range(3):
        filename = "csv/{}.csv".format(str(i))
        print(filename)
    
        try:
            df = pd.read_csv(filename)
            print(df)
        except FileNotFoundError as e:
            print("{} not found.".format(filename))
        print()
    
    # csv/0.csv
    #   column
    # 0  value
    # 1  value
    # 2  value
    #
    # csv/1.csv
    # csv/1.csv not found.
    #
    # csv/2.csv
    #   column
    # 0  value
    # 1  value
    # 2  value
    

    Share this for your friends.

    If you felt this article is useful, please share.

    にほんブログ村 IT技術ブログへ

    -Python
    -,

    © 2024 ITips