有没有一种方法可以使用glob.glob方法移动文件?

时间:2020-02-19 22:51:37

标签: python pandas automation movefile

我创建了一个微型项目,其中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")

0 个答案:

没有答案