I'm trying to write a code that loops back if the condition doesn't exist.
I'm working on the beginning of a program and I'm fairly new to python. I want my code to check if a file is valid, if it is continue to the next step, if it isn't request a new path to the file. Any ideas on the best way to do it? I would also like to check if the file type is correct, but haven't found code similar to what I need. But the main thing is getting it to loop, I think.
Right now I'm just ending after it returns if it the file was retrieved or not. I could copy and paste the exists statements again, but I know there must be a better way to do that. Any help would be appreciated.
# Imports OS module to allow interaction with underlying operating system
import os
# Imports time functions
import time
# Greet the user
print ("Welcome to Project Coded Phish.")
# Ask user for the path to the text file they would like to use
print ("Please provide a valid path to the chat log text (.txt) file.")
#save the path from the user input
path1 = input ()
exists = os.path.isfile(path1)
# if the file can be found
if exists:
print ("File was successfully retrieved.")
# if it isn't found
else:
print ("Please provide a valid path to the chat log text (.txt) file.")
path1 = input ()
It prints the correct words if the path was found. It just prints the "Please provide a valid path to the chat log text (.txt) file."