I am creating an application that takes the input of pints of blood donated each week. It writes the information into a text file as well as retrieves the information. I want to put in some sort of error handling for when the user inputs an invalid file name. This is what I have tried:
findingFile = 1 while (findingFile == 1): fileName = input("Select a file to read.") try: inFile = (fileName, 'a') findingFile = 0 except: print ('File not found, please select a valid file.')
I test this code and this is the error that I get:
Select a file to read.adfadfadf Traceback (most recent call last): File "C:/Users/riley/Documents/Lab 10-3.py", line 98, in <module> main() File "C:/Users/riley/Documents/Lab 10-3.py", line 29, in main readFromFile(averagePints, pints) File "C:/Users/riley/Documents/Lab 10-3.py", line 85, in readFromFile str1 = inFile.read() AttributeError: 'tuple' object has no attribute 'read' Process finished with exit code 1
Could someone give me an idea of how to deal with this error handling?