将文件夹的文件复制到其它文件

def copyfile(target_dir, dir_path):
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)
    for root, dirs, files in os.walk(target_dir, topdown=True):
        path = root.replace(target_dir, dir_path)
        for dir in dirs:
            if not os.path.exists(os.path.join(path, dir)):
                os.makedirs(os.path.join(path, dir))
        for name in files:
            shutil.copy(os.path.join(root, name), os.path.join(path, name))

srcpath = r'D:\workspeces\报表'
today = datetime.today().date().strftime("%Y%m%d")
dir_path = os.path.join(r'D:\workspeces\报表历史文件', today)
copyfile(srcpath, dir_path)
print(str(srcpath)+"文件夹下数据已复制完成至"+str(dir_path))

猜你喜欢

转载自blog.csdn.net/xiaoyurainzi/article/details/130710634