I'm fairly new to python. I'm trying to create a function called updates. One of its arguments is a file name, called countryFileName. In my main.py I would prompt the user to enter the file name, but here in this function I need to make sure that the file name given by the user exists. If it does exist, it should then proceed to process the file. If the country file does not exist, then updates should give the user the option to quit or not, prompting y or n. If the user does not wish to quit (responds with “n”), then the function should prompt the user for the name of a new file. If the user wishes to quit (responds with a “y” or anything other than a “n”, then the method should exit and return False. The method should continue to prompt for file names until a valid file is found or the user quits. Here's what I got so far:
def updates(countryFileName):
file3 = open(countryFileName, "r")
output = open("output.txt", "w")
if file3 == None:
# Run the loop until the user enters correct file name
while True:
# Prompt the user to enter the choice
quitchoice = input("Quit? Press y for yes or y for no: ")
if quitchoice.lower() != "n":
output.write("The update is unsuccessful, sorry\n")
output.close()
return False
elif quitchoice.lower() == "n":
countryFileName = input("Enter new name of file: ")
# Open the file and check status
file2 = open(countryFileName, "r")
if file2 != None:
break
Right now if the user enters the wrong file name, I just get an error "No such file or directory". I'm not sure why, but I think when python gets an error at the second line file = open(countryFileName, "r") it doesn't continue to my loop. How would I solve this?