将 Excel 从源复制到目标

Copy Excel from Source to Destination

我正在尝试复制源目录中以 XXXXX 开头的 excel 文件,并将它们放入与 excel 文件的第一个 XXXXX 匹配的目标目录中。我也只想匹配目标文件夹的第一个 XXXXX。将文件放入目标文件夹后,我想更改名称。下面的代码只允许我将 src excel 放到一个只命名为 XXXXX 的文件夹中,而不是(例如 'XXXXX FOLDER'),它只重命名根目录中的文件。想法?

for root, dirs, files in os.walk(dir_src):
    for file in files:
        if file.endswith(".xlsx"):
            filename = file[:5]
            shutil.copy((dir_src +'\'+file), (dir_dst + '\'+ filename))
            for filename in glob.glob(os.path.join(dir_dst + '\'+ filename, '*.xlsx')):
                    os.rename(filename, 'Weekly_Claims.xlsx') 

我想我明白了...虽然外面的人可能有更 pythonic 的方式来做到这一点

import glob,os   
import shutil
import re

for root, dirs, files in os.walk(dir_src):
    for file in files:
        if file.endswith(".xlsx"):
            filename = file[:5]
            src_file = os.path.join(dir_src, file)         
            #search destination subdirectory for folder 
            for root, dirs, files in os.walk(des_test):
                for name in dirs:
                    #if the source filename equals first 5 of the subdirectory name, copy to the des_file
                    if filename == name[:5]:
                        dst_file = os.path.join(des_test, name)
                        shutil.copy(src_file, dst_file)