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!