在两个项目中可能要复用文件,源目录下的文件改动后要更新到目标目录下,而经过整理后目标文件可能在不同的子目录中。
今天在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开源