Firstly, here is my code ::
def tryfirst():
try:
f = open('testfile.txt')
except FirstNotFound as e:
trysecond()
else:
print(f.read())
f.close()
def trysecond():
try:
f = open('test_file.txt')
except StillNotFound as e:
print(e)
else:
print(f.read())
f.close()
tryfirst()
What am I trying to accomplish?
I would like my program to try to open a text file that doesn't exist (testfile.txt), and after it tries that one, if it fails it moves onto the next one that does exist (test_file.txt) and prints the contents of the file. I want it to initiate the program by calling the first function, tryfirst, as done at the bottom of the code.
When I try to run this, I get the following and not sure why ::
NameError: global name 'FirstNotFound' is not defined
FirstNotFound
What is this?IOError
? Did you define those exceptions previously by subclassing the exception class?NameError
. Also,open
wouldn't throw them anyway... Have you tried to see what exception is actually thrown when you try to open a file that doesn't exist?FirstNotFound/StillNotFound
withIOError