I am having a problem with a function. The purpose of the function is to search through a large list for exact matches of strings. If the string ending matches one of the strings in the all_list, then that string is appended to name_list.
the problem is that once I find something, and the list is not empty, it should print "Found Something!" and return the list for use in other functions
if there is no match to the string being entered, then it should print "No File Found" and then ask the user again to input a different name. I did this by recursively calling the function.
If the user enters a correct file name the first time, then it works properly. But if the user enters a wrong file name, it will eventually find something but will return nothing
output:
Enter a file name: data.rtf
Found Something!
the problem is that once I find something, and the list is not empty, it should print "Found Something!" and return the list for use in other functions
if there is no match to the string being entered, then it should print "No File Found" and then ask the user again to input a different name. I did this by recursively calling the function.
If the user enters a correct file name the first time, then it works properly. But if the user enters a wrong file name, it will eventually find something but will return nothing
def search_by_name(all_list): #returns list of strings
try:
name_list = []
name = str(input("\nEnter a file name: ")).strip()
for i in all_list:
if i[i.rfind("/")+1:] == name:
name_list.append(i)
if name_list == []:
print("No File Found")
search_by_name(all_list)
if name_list != []:
print("\nFound Something!")
return name_list
except:
search_by_name(all_list)
output:
Enter a file name: data.rtf
Found Something!