把不同文件夹中的文件合并到同一个文件夹中,并重命名
import os
import shutil
source_path = os.path.abspath(r'G:\各类明细表')
for root, dirs, files in os.walk(source_path):
if len(dirs) == 0:
name_1 = root.split("\\")[-1]
target_path = root.split("\\")[0] + "\\" + root.split("\\")[1] + "\\" + root.split("\\")[2]
for file in files:
if file.split(".")[-1] == "xlsx":
name_2 = file[0:-5]
name = name_2 + "_" + name_1 + ".xlsx"
elif file.split(".")[-1] == "xls":
name_2 = file[0:-4]
name = name_2 + "_" + name_1 + ".xls"
shutil.copy(os.path.join(root, file), os.path.join(target_path, name))
print(os.path.join(target_path, name))
print("*"*10, "全部复制完毕!", target_path, "*"*10)