I got stuck at looping through folders with os.walk. My code looks like this:
import os
from datetime import timedelta
startD = date(2020,7,10)
day= timedelta(days=1)
EndD = date(2020,7,13)
folder = 'somefolder'
while startD <= EndD:
date=(startD.strftime("%Y%m%d"))
file = date + 'somename'
file2 = date + 'somene2'
for dirpath, subdirs, files in os.walk(folder):
for f in files:
if file in f or file2 in f:
print(os.path.join(dirpath,f))
else:
print("no file for", date)
startD += day
I have a time period of 4 days (from start to end date with 1 day addition) and I want to find if there is any filename (file and file2) existing in my "folder". If file exist I want its full path to be printed, but in other case I want to be notified there is no such filename (file and file2) in folder.
I deliberately deleted "file" and "file2" (for 11.7.2020) from folder in order to check if else statement work, but if I run my code it prints "no file for" + date for EACH FILE IN FOLDER that is not exact as defined.
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
ETC....
I want to receive results like this (loop for date from 20200710 - 20200714):
path for 20200710 + 'somename'
path for 20200710 + 'somene2'
no file for 20200711 + 'somename'
no file for 20200711 + 'somene2'
path for 20200712 + 'somename'
path for 20200712 + 'somene2'
path for 20200713 + 'somename'
path for 20200713 + 'somene2'
path for 20200714 + 'somename'
path for 20200714 + 'somene2'
I am aware that loops through all files and its clear that is prints "no file" for each file that doesn't has exact filename. I would like to print out only EXACT filenames that are missing, not all of them.