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

10,165

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.

Share:
10,165
Robert Hieger
Author by

Robert Hieger

I am an aspiring web developer/designer and mobile apps developer/designer living in New York City. I am interested in a holistic approach to this pursuit, striving for well-defined skills in both the front- and back-end of these fields, prompting a professor at NYU Tandon School of Engineering to refer to me as a "Unicorn." I have a lifelong involvement in performing arts as an actor, writer, composer and director. Over the years, I have engaged in autodidactic pursuit of knowledge in computer science aimed at creative technology, therefore considering myself a Creative Technologist. As of 2011, I hold a BS in Integrated Web Applications and Design from the City University of New York Baccalaureate for Unique and Interdisciplinary Studies. In January 2017, I earned an MS in Integrated Digital Media from NYU Tandon School of Engineering. Fully cognizant of the need for lifelong learning and how rapidly technologies change, I am constantly filling the gaps in my own knowledge, and hope to be of service to others, wherever I might.

Updated on July 22, 2022

Comments

  • Robert Hieger
    Robert Hieger almost 2 years

    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

    • Karnage
      Karnage over 7 years
      Your indentation is incorrect at 'try' and in the first 'with open'.
  • matth
    matth over 7 years
    Did you find documentation for the existence of .filename?
  • boertel
    boertel over 7 years
    I would usually do a pdb.set_trace() and then dir(not_found) to have a list of all attributes/methods for an object.