Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Use Python FileNotFoundError to print name of missing file?

I have search high and low on he net for answers to this question. Perhaps it is simply a bit too specific, but here goes.

I am working through a crash course text on Python, and probably would be considered either an advanced beginner or a beginning intermediate practitioner at this point.

I am trying to use a try-catch block to display a message about a missing file, using an exception with the FileNotFoundError exception type, as follows:

filename_1 = 'cats.txt'
filename_2 = 'dogs.txt'

    try:
        with open(filename_1) as file_obj:
        contents = file_obj.read()
        contents = contents.split('\n')
        print('\n\tContents of File \'cats.txt\':\n')

        for line in contents:
            print('\t', line)

        print('\n')


        with open(filename_2) as file_obj:
            contents = file_obj.read()
            contents = contents.split('\n')
            print('\n\tContents of File \'dogs.txt\':\n')

            for line in contents:
            print('\t', line)

            print('\n')
    except FileNotFoundError:
        print('\n\tSorry, \'', FileNotFoundError.__filename__, '\' not found.\n')

I tried using an attribute name of filename, which obviously does not exist. But I have been able to find no appropriate attributes from which to extract this vital data for the error. As there are two different filenames specified, it could be either of those files that are not present.

Does Python provide this kind of functionality in its built-in exceptions? It would seem that it should.

Any help is greatly appreciated. Thanks.

Sincerely,

Robert Hieger

like image 867
Robert Hieger Avatar asked Nov 17 '16 23:11

Robert Hieger


People also ask

How do I resolve FileNotFoundError in Python?

The Python FileNotFoundError: [Errno 2] No such file or directory error is often raised by the os library. This error tells you that you are trying to access a file or folder that does not exist. To fix this error, check that you are referring to the right file or folder in your program.

How do you handle No such file or directory in Python?

The Python "FileNotFoundError: [Errno 2] No such file or directory" occurs when we try to open a file that doesn't exist in the specified location. To solve the error, move the file to the directory where the Python script is located if using a local path, or use an absolute path.

How do you solve No such file or directory?

In some cases, this error could be shown when the path of the specified file or folders exceeds 258 characters in length. The way to solve this is to reduce the length of the full path to the items specified, either by moving or renaming the file(s) and/or containing folders.


1 Answers

You can find the filename from the exception instance (not from the class).

try:
    # your code opening files, etc.
    # ...
except FileNotFoundError as not_found:
    print(not_found.filename)

This attribute is documented at the parent of FileNotFoundError, OSError.

like image 95
boertel Avatar answered Oct 17 '22 08:10

boertel