1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| from datetime import datetime, timedelta import os
def create_folder(st_time, end_time, root_folder=None): root_folder = "./test_folder" if root_folder is None else root_folder
while st_time < end_time: folder_name = f"{root_folder}/{st_time.year}/{st_time.month}/{st_time.day}/{st_time.hour}/{st_time.minute}" os.makedirs(folder_name) st_time += timedelta(minutes=1)
def find_folder_acc_time_range_v1(st_time, end_time, root_folder=None): root_folder = "./test_folder" if root_folder is None else root_folder lt = [] while st_time < end_time: folder_name = f"{root_folder}/{st_time.year}/{st_time.month}/{st_time.day}/{st_time.hour}/{st_time.minute}" if os.path.exists(folder_name): lt.append(folder_name) st_time = st_time + timedelta(minutes=1) return lt
def find_folder_acc_time_range_v2(st_time, end_time, root_folder=None): """ left close and right open """ root_folder = "./test_folder" if root_folder is None else root_folder lt = []
def get_by_level(t, level): if level == 0: return t.year elif level == 1: return t.month elif level == 2: return t.day elif level == 3: return t.hour elif level == 4: return t.minute else: assert False, "level is invalid"
def find(dir, level=0, eq_left=True, eq_right=True): if level == 5: lt.append(dir) return for sub_dir in os.listdir(dir): if not eq_left and not eq_right: find(f"{dir}/{sub_dir}", level + 1, False, False) else: st_num = get_by_level(st_time, level) end_num = get_by_level(end_time, level) dir_num = int(sub_dir) if (eq_left and dir_num < st_num): continue elif (eq_right and dir_num > end_num and level < 4) or (eq_right and dir_num >= end_num and level == 4): continue else: tel = eq_left and (st_num == dir_num) ter = eq_right and (dir_num == end_num) find(f"{dir}/{sub_dir}", level + 1, tel, ter)
find(root_folder) return lt
if __name__ == "__main__": st = datetime.strptime("2022-11-12 20:46", "%Y-%m-%d %H:%M") ed = datetime.strptime("2022-12-13 21:59", "%Y-%m-%d %H:%M") create_folder(st, ed) st = datetime.strptime("2022-11-12 11:58", "%Y-%m-%d %H:%M") ed = datetime.strptime("2022-12-12 11:59", "%Y-%m-%d %H:%M") res_v1 = find_folder_acc_time_range_v1(st, ed) res_v2 = find_folder_acc_time_range_v2(st, ed) print(set(res_v1) == set(res_v2)) print("res_v1 - res_v2", set(res_v1) - set(res_v2)) print("res_v2 - res_v1", set(res_v2) - set(res_v1))
|