1

I've created a mini project where python will :

  1. Pick and format xlsx files;
  2. Append new report to master file; and
  3. 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")
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Al19Rok92
  • 11
  • 1
  • `glob.glob()` returns a list, you need to loop over it and call `os.rename()` on each filename. – Barmar Feb 19 '20 at 23:16
  • Can you clarify what the issue is? – AMC Feb 19 '20 at 23:45
  • @AMC, so currently I need to identify the file name and use it specifically when moving file. The goal is to use something similar like glob.glob where I want to identify partial name and format, something like /path/ Part_file_name * to move it to archive directory. – Al19Rok92 Feb 20 '20 at 00:12
  • I want to schedule script to run on a weekly basis without my intervention, so when the new file drops into the folder ( file name contains dates), script runs without my need to place path and file names in os.rename(). I'm sorry if it's not clear enough, I'm trying my best =D – Al19Rok92 Feb 20 '20 at 00:14
  • Thanks @Barmar, that sounds like a neat solution! I will test it out! – Al19Rok92 Feb 20 '20 at 00:19

0 Answers0