0

我创建了一个迷你项目,python 将:

  1. 选择并格式化 xlsx 文件;
  2. 将新报告附加到主文件;和
  3. 将 xlsx 文件移动到最后的存档文件夹。

第一步和第二步我没有问题,我使用 glob glob 匹配路径和部分文件模式以进一步使用步骤 2。我在第三步中苦苦挣扎,我使用 os.rename 更改存档中的移动文件文件夹。我想找到与 glob.glob 函数类似的方法来移动具有匹配模式的文件,而不是某个文件,这样我就可以安排代码并自动化该过程。

请参阅附件中我的项目代码:

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")
4

0 回答 0