Python FileNotFoundError with Examples

Files in Python:

One of the most important subjects for programmers and automation testers is Python file handling (also known as File I/O). It is necessary to work with files in order to write to or read data from them.

Also, if you didn’t know, I/O operations are the most expensive processes where a programme can go wrong. As a result, you must use extreme caution while implementing file processing for reporting or any other reason. Optimizing a single file action can help in the creation of a high-performing application or a reliable automated software testing solution.

Consider the following scenario: you’re planning to construct a large Python project with a large number of workflows. Then it’s unavoidable that you don’t make a log file. You’ll also be handling the log file’s read and write activities. Debugging huge applications with log files is a terrific way to go. It’s usually better to consider a scalable design from the start, as you won’t be sorry later if you didn’t.

FileNotFoundError in Python:

“FileNotFoundError” is a Python error that occurs when a file does not exist and we attempt to use it.

Let us see the below examples where there is no file say “samplefile.txt” in the memory but we try to access it and handle the exception.

FileNotFoundError with Examples in Python

Example1

Approach:

  • Open the file in a try block.
  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
  • Print some random text for acknowledgment.
  • Check if the FileNotFoundError occurred or not using the except block.
  • If it is true, then print some random text for acknowledgment.
  • Else print some random text for acknowledgment in another except block. (It is some other error)
  • The Exit of the Program.

Below is the implementation:

# Open the file in try block
try:
    # Make a single variable to store the path of the file. This is a constant value.
    # This value must be replaced with the file path from your own system in the example below.
    givenFilename = "samplefile.txt"
    # Open the file in read-only mode. In this case, we're simply reading the contents of the file.
    gvn_file = open(givenFilename, 'r') 
    # Print some random text for acknowledgment
    print("Your file is opened now!!!")
# Check if the FileNotFoundError occurred or not using the except block
except FileNotFoundError:
    # If it is true, then print some random text for acknowledgment
    print("sorry, there is no such file")
# Else print some random text for acknowledgment in another except block
except:
    print("It is some other error")

Output:

sorry, there is no such file

Example2 – using fopen function instead of open

In this case, I just typed “fopen” instead of “open,” which is an error in Python because there is no such function as “fopen.”

Approach:

  • Open the file in a try block.
  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open the file in read-only mode using the fopen() function(no such function). In this case, we’re simply reading the contents of the file.
  • Print some random text for acknowledgment.
  • Check if the FileNotFoundError occurred or not using the except block.
  • If it is true, then print some random text for acknowledgment.
  • Else print some random text for acknowledgment in another except block. (It is some other error)
  • The Exit of the Program.

Below is the implementation:

# Open the file in try block
try:
    # Make a single variable to store the path of the file. This is a constant value.
    # This value must be replaced with the file path from your own system in the example below.
    givenFilename = "samplefile.txt"
    # Open the file in read-only mode using the fopen() function(no such function).
    # In this case, we're simply reading the contents of the file.
    gvn_file = fopen(givenFilename, 'r') 
    # Print some random text for acknowledgement
    print("Your file is opened now!!!")
# Check if the FileNotFoundError occured or not using the except block
except FileNotFoundError:
    # If it is true, then print some random text for acknowledgement
    print("sorry, there is no such file")
# Else print some random text for acknowledgement in another except block
except:
    print("It is some other error")

Output:

It is some other error

Google Colab Images:

Files and Code: