I've created a mini project where python will :
- Pick and format xlsx files;
- Append new report to master file; and
- Move the xlsx file to the archive folder at the end.
I have no problems with the first and second steps, I use glob glob to match the path and part file patterns to further work with step 2. I am struggling with the third step where I use os.rename to change move file in the archive folder. I would like to find similar way as glob.glob function to move file with a matching pattern, not a certain file so I can schedule code and automate the process.
Please see the code of my project attached:
import glob
import pandas as pd
import os
# STEP 1 PREPARATION
g = glob.glob('/path/part file name *')
df = pd.concat([pd.read_excel(s, skiprows=2, header=0) for s in g], ignore_index=True)
print("Step 1 Deployed: File successfully read and Branch Names were allocated;")
# STEP 2 JOIN NEW REPORT TO MASTER FILE
print("Step 2 Process Preparation: Loading Master File into the logic;")
md = pd.read_excel(master, header=0)
print("Step 2 Process Begins: new report appends to SOH master file;")
mega = md.append(df, sort=False)
mega.to_excel('/pathtodropxlsx/ filename.xlsx',
sheet_name="Data", index=False)
print("Step 2 Finalised: File uploaded;")
# STEP 3 MOVE WEEKLY REPORT TO THE ARCHIVE FOLDER
print("Step 3 initiation: Move new report file in Archive folder")
New_report = '/path/part file name.xlsx' #<--This is the area where I am having a trouble thinking.
#I basically need to get the filename used in step 1 where I
# used glob.glob function to identify new file.
file_name = New_report.partition('reporting/')[2]
str(file_name)
os.rename(New_report, "/newpath/archive/" + file_name)
print("REPORT DATA IS UP TO DATE")