File IO with Exception Handling
try:
with open('/File/Location/File_name.txt', mode='r') as my_file:
print(my_file.read())
except:
print("File does not exist: FileNotFoundError")
print('This line was run...')You can also have specific cases:
try:
with open('/File/Location/File_name.txt', mode='r') as my_file:
print(my_file.read())
except TypeError:
print("There was a type error")
except FileNotFoundError:
print("File does not exist: FileNotFoundError")
except:
print("Different types of error from the predicted ones")
print('This line was run...')There is also an additional clause called finally
finallyIf you are dealing with files there are sometimes errors that variables are not accessible
Last updated