文件替换备份脚本

# -*- coding: utf-8 -*-
'''
author:VemboLiu
description:backup files while replacing
date:2022.04.19
'''

import os
import shutil

#输入文件信息
file_new_route = input("New files route:")
file_ori_route = input("Original file route:")
file_backup_route = input("Backup file route:")


file_oridirect = file_ori_route
file_bacdirect = file_backup_route
#查找新文件层次
for root,dirs,files in os.walk(file_new_route):
    print("----------root is %s"%root)
    #获取当前遍历的相对路径
    cur_path_rel = root[len(file_new_route):]
    print("cur_path_rel is %s"%cur_path_rel)
    file_oridirect = r"%s%s"%(file_ori_route,cur_path_rel)
    file_bacdirect = r"%s%s"%(file_backup_route,cur_path_rel)

    #对文件进行处理
    if not os.path.exists(file_bacdirect):
        os.makedirs(file_bacdirect)
        print("New folder has created %s"%file_bacdirect)
    if not os.path.exists(file_oridirect):
        os.makedirs(file_oridirect)
        print("New folder has created %s"%file_oridirect)
    for file in files:
        file_oripath = r"%s\%s"%(file_oridirect,file)
        file_bacpath = r"%s\%s"%(file_bacdirect,file)
        file_newpath = r"%s\%s"%(root,file)
        print("file_oripath is %s"%file_oripath)
        if os.path.exists(file_oripath):
            shutil.copy(file_oripath,file_bacpath)
            print("Backup file %s success" % file)
        shutil.copy(file_newpath, file_oripath)
        print("Copy file %s success"%file)

替换文件时备份之前版本文件,用于补丁替换等~。
选择新文件的文件夹、旧文件的文件夹及备份的文件夹。
如要将D:\UNM2000\server\etc里面的部分文件替换为D:\Newfolder中的文件,
则original选择D:\UNM2000\server\etc,
new选择D:\Newfolder,
backup选择要备份到的文件夹即可

posted @   柳然  阅读(53)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示