Reputation: 51
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."
Upvotes: 0
Views: 102
Reputation: 45
You can achieve this with a recursive function like so:
# 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.")
# if the file can be found
def check():
path1 = input ()
exists = os.path.isfile(path1)
if exists:
print ("File was successfully retrieved.")
return
# if it isn't found
else:
print("file not found")
check()
check()
Upvotes: 0
Reputation: 36
Try this:
while True:
path1 = input()
exists = os.path.isfile(path1)
if exists and path1.endswith('.txt'):
print("File was successfully retrieved.")
with open(path1) as file:
# do something with file
break
else:
print("Please provide a valid path to the chat log text (.txt) file.")
While loop will keep on looping until break statement. Part of code that starts with 'with' is called context manager and is used for opening files. endswith method will check extension of a file.
Upvotes: 0
Reputation: 96
You could try
import os
def ask_for_filepath():
input_path = input("Please provide a valid path to the chat log text (.txt) file.")
return input_path
input_path = ask_for_filepath()
while os.path.isfile(input_path) is False:
input_path = ask_for_filepath()
Upvotes: 0
Reputation: 4482
This could easyly be done with a while loop:
while True:
exists = os.path.isfile(path1)
# if the file can be found
if exists:
print ("File was successfully retrieved.")
# since condition is met, we exit the loop and go on with the rest of the program
break
# if it isn't found
else:
print ("Please provide a valid path to the chat log text (.txt) file.")
path1 = input ()
Upvotes: 1
Reputation: 6206
Try this:
path1 = input ()
while not os.path.isfile(path1):
print ("Please provide a valid path to the chat log text (.txt) file.")
path1 = input ()
print ("File was successfully retrieved.")
Upvotes: 3