I have a directory with files that are deposited by Power Automate and named by the date of the pull. Using Python I've been able to successfully move these files from one directory to another by pattern matching. I need it to be more dynamic, and use the logic I enuerate earlier in script to grab the previous 10 days' files and move only those into a temp directory (and from there to dataframe, then aggregate for timeline summariess in various BI reports). I am having difficulty with that transition from defining the pattern within the script (pattern = mastdirectory + "/01*") to defining the pattern based on each line item in res [], the list that holds the dates (relative to the current date (today)). Any suggestions are appreciated.
import pandas as pd
import os
import time
from asyncio import sleep
import win32com.client
import time
import glob
from datetime import datetime as dt
from datetime import timedelta
import shutil as shutil
mastdirectory = r"C:/Users/bowergr/OneDrive - Company Name/MAST Pulls/History/MAST Library"
dir_cache = r"C:/Users/Bowergr/OneDrive - Company Name/MAST Pulls/Tracking/QMS/Data_Cache/Mast_Transfer//"
today = dt.today()
today_date, today_time = today.date(), today.time()
D = 10
print("Current Date: ", today_date.strftime('%m-%d-%Y'))
today_date_f = today_date.strftime('%m-%d-%Y')
daterange = today_date + timedelta(days=-10)
daterange_f = daterange.strftime('%m-%d-%Y')
print("Date Range: (Current Date: ", today_date.strftime('%m-%d-%Y'),
"), (Last Date:", daterange_f, ")")
res = []
for day in range(D):
date = (today_date + timedelta(days = -day)).strftime('%m-%d-%Y')
res.append(date)
print("MAST Evaluation Dates: " + str(res))
pattern = mastdirectory + "/01*"
for file in glob.iglob(pattern, recursive=True):
# filename extract from path
file_name = os.path.basename(file)
shutil.copyfile(file, dir_cache + "_" + file_name)
print('Moved:', file)
I've tried using with os.scandir() with a loop but that failed badly. Tried something like:
with os.scandir(mastdirectory) as dirs: for entry in dirs: if entry in res: print(entry) else: print("n/a")
However when I rewound myself back a few steps I managed to come up with the script that is working, but not dynamically evaluating against each item in the list, just pattern = mastdirectory + "/01*"
res[] looks like this:
['01-12-2023', '01-11-2023', '01-10-2023', '01-09-2023', '01-08-2023', '01-07-2023', '01-06-2023', '01-05-2023', '01-04-2023', '01-03-2023']
The filenames look identical.
'01-12-2023.xlsx', '01-11-2023.xlsx'