I'm trying to create a code that will use a function to check if the file exists and if not then it will ask the user for a file name again. This loop should continue until an existing file name is given. I managed to do the same concept using a function to check if the first input was an integer but I can't seem to replicate it for the file name portion without getting an error(FileNotFoundError: [Errno 2] No such file or directory:) and ending the loop. (it still prints the "not valid file" bit but it ends in an error)
Here's a snippet from my code:
def checkInt(val):
try:
val = int(val)
return val
except:
print('Not a valid integer')
def checkFile(fileName):
try:
File = open(fileName)
File.close
except:
print('Not a valid file.')
def main():
print('Hi welcome to the file processor')
while 1:
val = checkInt(input('''Selection Menu:
0. Exit program
1. Read from a file
'''))
if val == 0:
print('goodbye')
quit()
elif val == 1:
fileName = input('Enter a file name: ')
checkFile(fileName)
inFile = open(fileName,'r')
print(inFile.read())
inFile.close
main()
I feel like its an obvious mistake and I greatly appreciate the insight!