[Tutor] Getting the real exception clause

Jim Mooney cybervigilante at gmail.com
Sun Jun 16 22:26:11 CEST 2013


'''I'm using general Exception to print out what the exception is
until I learn them, but
it will print out "[Errno 2] No such file or directory" in this case,
when the real
exception I'd need to use in an except clause is FileNotFoundError.
How do I get the
exception I need to use in the except clause from the more English-like message?
'''

#Using C:\Python33\python.exe on Win 7 in c:\python33\jimprogs
try:
    fh = open('nosuchdirectory/text/truthyfalsey.txt')
    for line in fh:
        print(line,end='')
    fh.close()
except Exception as err:
    print(err)

Jim
After indictment the bacon smuggler was put on the no-fry list


More information about the Tutor mailing list