Created
October 18, 2013 08:15
-
-
Save xalexchen/7038205 to your computer and use it in GitHub Desktop.
This is a small python script to help you synchronize two folders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding=utf-8 | |
# This is a small python script to help you synchronize two folders | |
# Compatible with Python 2.x and Python 3.x | |
import filecmp, shutil, os, sys | |
SRC = r'C:/a' | |
DEST = r'C:/b' | |
IGNORE = ['Thumbs.db'] | |
def get_cmp_paths(dir_cmp, filenames): | |
return ((os.path.join(dir_cmp.left, f), os.path.join(dir_cmp.right, f)) for f in filenames) | |
def sync(dir_cmp): | |
for f_left, f_right in get_cmp_paths(dir_cmp, dir_cmp.right_only): | |
if os.path.isfile(f_right): | |
os.remove(f_right) | |
else: | |
shutil.rmtree(f_right) | |
print('delete %s' % f_right) | |
for f_left, f_right in get_cmp_paths(dir_cmp, dir_cmp.left_only+dir_cmp.diff_files): | |
if os.path.isfile(f_left): | |
shutil.copy2(f_left, f_right) | |
else: | |
shutil.copytree(f_left, f_right) | |
print('copy %s' % f_left) | |
for sub_cmp_dir in dir_cmp.subdirs.values(): | |
sync(sub_cmp_dir) | |
def sync_files(src, dest, ignore=IGNORE): | |
if not os.path.exists(src): | |
print('= =b Please check the source directory was exist') | |
print('- -b Sync file failure !!!') | |
return | |
if os.path.isfile(src): | |
print('#_# We only support for sync directory but not a single file,one file please do it by yourself') | |
print('- -b Sync file failure !!!') | |
return | |
if not os.path.exists(dest): | |
os.makedirs(dest) | |
dir_cmp = filecmp.dircmp(src, dest, ignore=IGNORE) | |
sync(dir_cmp) | |
print('^_^ Sync file finished!') | |
if __name__ == '__main__': | |
src, dest = SRC, DEST | |
if len(sys.argv) == 3: | |
src, dest = sys.argv[1:3] | |
sync_files(src, dest) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That was great :)