1

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' 
wwii
  • 23,232
  • 7
  • 37
  • 77
bowergr
  • 11
  • 2
  • What do the file names look like? What does `res` look like after it is populated? All of the files are *in* `mastdirectory`? – wwii Jan 13 '23 at 00:04
  • mastdirectory is the source folder where the files are located, dir_cache is the destination folder where i'm moving the files that match the dates in res-- 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 idential. '01-12-2023.xlsx', '01-11-2023.xlsx' etc. -- – bowergr Jan 13 '23 at 00:14
  • Does [Build the full path filename in Python](https://stackoverflow.com/questions/7132861/build-the-full-path-filename-in-python) answer your question? Just iterate over `res` and build the filename/path. – wwii Jan 13 '23 at 00:24
  • iterating over res is my issue. I've tried to make it work but have been unsuccessful in doing so properly. My understanding of python is still fairly low. can you provide an example I could try working from? – bowergr Jan 13 '23 at 17:53
  • Did you review the link in my previous comment? [https://docs.python.org/3/tutorial/controlflow.html#for-statements](https://docs.python.org/3/tutorial/controlflow.html#for-statements). – wwii Jan 13 '23 at 18:07
  • Here is another [Appending the same string to a list of strings in Python](https://stackoverflow.com/questions/2050637/appending-the-same-string-to-a-list-of-strings-in-python). – wwii Jan 13 '23 at 18:17
  • reviewing -- ty – bowergr Jan 13 '23 at 18:17

1 Answers1

0

There are many ways to concatenate strings (dir,name,ext) but when constructing file paths I like to use pathlib.

from pathlib import Path

mastdirectory = r"C:/Users/bowergr/OneDrive - Company Name/MAST Pulls/History/MAST Library"
res = ['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']

fromdir = Path(mastdirectory)

for name in res:
    path = fromdir.joinpath(name).with_suffix('.xlsx')
    print(path)
    # copy or move
wwii
  • 23,232
  • 7
  • 37
  • 77