Finding file based on user input?

46 Views Asked by At

I printed files using os.listdir() and found the files inside the folder i am looking for. I'm now looking for user input to know which files to look in. The files are in years and month and I'm not sure how?

Picture of my code now

2

There are 2 best solutions below

2
On

Are you expecting a year and month as input from the user? In that case you could parse the input string, then use some kind of regex matching to find the corresponding file.

3
On

Could try using this in that case:

year = input("Enter year :")
month = input("Enter day :")
date = year + month
found = False
for file_name in a:
    if date in file_name:
        print("File found!")
        found = True
        break
if not found:
    print("File not found")