import os from datetime import datetime, timedelta from localconfig import STORESFILE, SORTED_PATH, log import tempfile import csv stores_to_invoice = {} def list_of_stores_on(YYYY, MM, DD): list_of_stores_in_folder = [] folder = os.path.join(SORTED_PATH, YYYY, MM, DD) for root, dirs, files in os.walk(folder): for dir in dirs: if len(dir) == 4: list_of_stores_in_folder.append(dir) return list_of_stores_in_folder def get_new_stores(stores_in_sorted): # open the csv file and crosslink it with the found stores in the folder new_stores = '' with open(STORESFILE, newline="", encoding="utf-8") as f_in: reader = csv.DictReader(f_in) for row in reader: if row["store"] in stores_in_sorted: stores_in_sorted.remove(row["store"]) # now we are left with stores_in_sorted only holding stores not in our storesfile if len(stores_in_sorted) > 0: for s in stores_in_sorted: new_stores += f"{s},,,,,1\n" log(f"Found new store {s}. You can find it in {STORESFILE}",1) return new_stores if __name__ == '__main__': today = datetime.today().date() yesterday = today - timedelta(days=1) stores_poslogged = list_of_stores_on(yesterday.strftime('%Y'), yesterday.strftime('%m'), yesterday.strftime('%d')) csv_lines = get_new_stores(stores_poslogged) if len(csv_lines) > 1: with open(STORESFILE, "a", encoding="utf-8") as f: f.write(csv_lines)