我如何 shutil.move 我的文件,post 处理,在 python 中基于文件名?

How do I shutil.move my file, post processing, in python based on filename?

我的代码-

import pandas as pd
import datetime as dt
import os
import shutil
    path = "C:/Users/rocky/Desktop/autotranscribe/python/Matching"
destination_path = ('C:/Users/rocky/Desktop/autotranscribe/python/Matching/Processed')
    for file in os.listdir("C:/Users/rocky/Desktop/autotranscribe/python/Matching"):
        if file.startswith("TVC")and "(updated headers)" not in file:
            dfs = pd.read_excel(file, sheet_name=None)
            output = dict()
            for ws, df in dfs.items():
                if ws in ["Opt-Ins", "New Voting Members", "Temporary Members"]:
                    continue
                if ws in ["Voting Members", "Removed Members"]:
                    temp = df
                    dt = pd.to_datetime(os.path.getctime(os.path.join(path,file)),unit="s").replace(nanosecond=0)
                    temp['Status'] = "Active" if ws == "Voting Members" else "Cancelled"
                    output[ws] = temp
            writer = pd.ExcelWriter(f'{file.replace(".xlsx","")} (updated headers).xlsx')
            for ws, df in output.items():
                df.to_excel(writer, index=None, sheet_name=ws)
            writer.save()
            writer.close()
shutil.move(path, destination_path )

我只想在处理完我的所有代码并生成输出文件后,将这里正在处理的文件 if file.startswith("TVC")and "(updated headers)" not in file: 移动到另一个文件夹目录..我在哪里申请 shutil.move ?我把它放在我的内部循环之外,但文件没有按预期填充到我的 dest 文件夹中。我需要一个 if 语句吗?如果脚本成功运行并生成输出 xls,则将原始文件移动到目标文件夹?

有几个问题。如果您想根据在 for 循环中获得的名称移动文件,则需要在 for 循环中进行移动。

一个更大的问题是 os.listdir 只是 returns 个文件名。如果要打开或访问该文件,则需要将路径添加回去。

你的temp = df东西没有意义。只需更改 df.

import pandas as pd
import datetime as dt
import os
import shutil

path = "C:/Users/rocky/Desktop/autotranscribe/python/Matching"
destination_path = path + '/Processed'
for file in os.listdir(path)
    if file.startswith("TVC") and "(updated headers)" not in file:
        fullpath = path + '/' + file
        dfs = pd.read_excel(fullpath, sheet_name=None)
        output = dict()
        for ws, df in dfs.items():
            if ws in ["Voting Members", "Removed Members"]:
                dt = pd.to_datetime(os.path.getctime(os.path.join(path,file)),unit="s").replace(nanosecond=0)
                df['Status'] = "Active" if ws == "Voting Members" else "Cancelled"
                output[ws] = df
        writer = pd.ExcelWriter(fullpath.replace(".xlsx","(updated headers).xlsx")
        for ws, df in output.items():
            df.to_excel(writer, index=None, sheet_name=ws)
        writer.save()
        writer.close()
        shutil.move(fullpath, destination_path )

考虑到您问题中的代码试图做什么,您可能需要考虑构建功能 and/or 稍微修改文件创建策略。

这里有两种可能的策略。

#1:创建新文件,然后移动它。

#2:在目标目录中就地创建新文件。

无论哪种情况,还可以考虑以下相关问题:

  • 目标目录不存在怎么办?您可以允许 python 标准库调用的默认行为(引发异常),或者您可以创建目录。
  • 如果目标文件已经存在怎么办?您可以允许默认标准库行为(引发异常),或覆盖它(静默或发出警告),或在将新文件放入目标目录之前重命名现有文件。

例如,下面的代码使用策略 #1(创建和移动文件),根据需要创建目标目录,并用警告覆盖任何现有文件。 (出于测试目的,我更改了源路径和目标路径的名称。)

import pandas as pd
#import datetime as dt
import os
import shutil
#path = "C:/Users/rocky/Desktop/autotranscribe/python/Matching"
#destination_path = ('C:/Users/rocky/Desktop/autotranscribe/python/Matching/Processed')
path = "."
destination_path = ('./Processed')
for file in os.listdir(path):
    if file.startswith("TVC")and "(updated headers)" not in file:
        dfs = pd.read_excel(file, sheet_name=None)
        output = dict()
        for ws, df in dfs.items():
            if ws in ["Opt-Ins", "New Voting Members", "Temporary Members"]:
                continue
            if ws in ["Voting Members", "Removed Members"]:
                temp = df
                #dt = pd.to_datetime(os.path.getctime(os.path.join(path,file)),unit="s").replace(nanosecond=0)
                temp['Status'] = "Active" if ws == "Voting Members" else "Cancelled"
                output[ws] = temp

        destination_file = f'{file.replace(".xlsx","")} (updated headers).xlsx'
        destination_file_with_path = f'{destination_path}/{file.replace(".xlsx","")} (updated headers).xlsx'

        # Create and move file, create the destination directory if needed, and overwrite any existing file with a warning:
        writer = pd.ExcelWriter(destination_file)
        for ws, df in output.items():
            df.to_excel(writer, index=None, sheet_name=ws)
        writer.save()
        writer.close()
        if not os.path.exists(destination_path):
            os.makedirs(destination_path)
        if os.path.exists(destination_file_with_path):
            print(f'{destination_file_with_path} already exists, overwriting')
            os.remove(destination_file_with_path)
        shutil.move(destination_file, destination_path)

或者,如果您想使用策略 #2(在其目标目录中创建文件)如果需要再次创建目标目录并覆盖任何现有文件并发出警告,您可以这样做(注意 shutil 是这种方法不需要):

import pandas as pd
#import datetime as dt
import os
#import shutil
#path = "C:/Users/rocky/Desktop/autotranscribe/python/Matching"
#destination_path = ('C:/Users/rocky/Desktop/autotranscribe/python/Matching/Processed')
path = "."
destination_path = ('./Processed')
for file in os.listdir(path):
    if file.startswith("TVC")and "(updated headers)" not in file:
        dfs = pd.read_excel(file, sheet_name=None)
        output = dict()
        for ws, df in dfs.items():
            if ws in ["Opt-Ins", "New Voting Members", "Temporary Members"]:
                continue
            if ws in ["Voting Members", "Removed Members"]:
                temp = df
                #dt = pd.to_datetime(os.path.getctime(os.path.join(path,file)),unit="s").replace(nanosecond=0)
                temp['Status'] = "Active" if ws == "Voting Members" else "Cancelled"
                output[ws] = temp

        destination_file = f'{file.replace(".xlsx","")} (updated headers).xlsx'
        destination_file_with_path = f'{destination_path}/{file.replace(".xlsx","")} (updated headers).xlsx'

        # Create the destination directory if needed, create the new file in the destination directory, and overwrite any existing file with a warning:
        if not os.path.exists(destination_path):
            os.makedirs(destination_path)
        if os.path.exists(destination_file_with_path):
            print(f'{destination_file_with_path} already exists, overwriting')
            os.remove(destination_file_with_path)
        writer = pd.ExcelWriter(destination_file_with_path)
        for ws, df in output.items():
            df.to_excel(writer, index=None, sheet_name=ws)
        writer.save()
        writer.close()