批量恢复文件的Py脚本

在两个项目中可能要复用文件,源目录下的文件改动后要更新到目标目录下,而经过整理后目标文件可能在不同的子目录中。

今天在Win7虚拟机中使用xcopy srcpath destpath /U/Y同步Mac下的两个项目时,文件全变为小写了,因此想写个脚本自动同步、保留大小写。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# sync.py by rhcad

import os, sys, shutil

def replacefiles(srcdir, dstdir, fn):
    dstfile = os.path.join(dstdir, fn.lower())
    if os.path.isfile(dstfile) and os.path.exists(dstfile):
        os.remove(dstfile)
        shutil.copy(os.path.join(srcdir, fn), os.path.join(dstdir, fn))
        print(dstfile)
        return
    for fn2 in os.listdir(dstdir):
        dstfile = os.path.join(dstdir, fn2)
        if os.path.isdir(dstfile):
            replacefiles(srcdir, dstfile, fn)

if __name__=="__main__":
    srcdir = sys.argv[1]
    dstdir = os.path.abspath('.')
    for fn in os.listdir(srcdir):
        replacefiles(srcdir, dstdir, fn)

运行 python sync.py srcpath/somepath 就恢复文件名大小写了,以后可自动同步到任意子目录!

此文件已在GitHub开源

转载于:https://my.oschina.net/rhcad/blog/264290

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值