#Author: James Tam #Version: November 1, 2024 #Learning objective: #*Recovering from a file IO run time error using exceptions #*Note: # -The example is purposely as simple as possible so it doesn't handle # things as well as it should. # -But it won't crash if problems occur during the opening, reading or # even the closing of the file. #Trying to open or read from a file may result in an exception being raised. fileOK = False while(fileOK == False): try: filename = input("Name of file to open: ") aFile = open(filename,"r") for line in aFile: print(line,end="") aFile.close() except IOError: print("Problem encounted during the interaction with %s" %(filename)) else: fileOK = True print() print("Done reading from file")