How Can I Use Python FileNotFoundError to print name of missing file?
<p>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.</p>

<p>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.</p>

<p>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:</p>

<pre><code>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')
</code></pre>

<p>I tried using an attribute name of <strong>filename</strong>, 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.</p>

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

<p>Any help is greatly appreciated. Thanks.</p>

<p>Sincerely,</p>

<p>Robert Hieger</p>