0

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

Magnmi
  • 1
  • 1

2 Answers2

0

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.

m8nkeyboi
  • 1
  • 2
  • Im first asking for year, if the year is in the folder im asking for the month. If not im telling the user the year/month is not avalible. Im new to python so i dont know how – Magnmi Oct 19 '22 at 09:48
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 21 '22 at 09:43
0

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")
m8nkeyboi
  • 1
  • 2
  • When i put the code in, it outputs "File not found" even if i put in the correct year and month – Magnmi Oct 20 '22 at 06:38
  • could you go with endswith( date + ".csv") since all the files ends with .csv and it is following the date? – Magnmi Oct 20 '22 at 06:58
  • I have come to the point where i find the file but im using the command file[0]. then im finding fil nr. 1 but not nr. 2 if i want nr.2. how can i fix that? – Magnmi Oct 20 '22 at 08:40