Skip to content

Instantly share code, notes, and snippets.

@seven332
Created February 21, 2018 08:37
Show Gist options
  • Save seven332/6b105aaf2096e84b492cec0c92cee9e4 to your computer and use it in GitHub Desktop.
Save seven332/6b105aaf2096e84b492cec0c92cee9e4 to your computer and use it in GitHub Desktop.
import argparse
import os
import filecmp
import shutil
def dejoin(path, opath):
if path.startswith(opath):
path = path[len(opath):]
if len(path) >= 1 and path[0] == '\\' or path[0] == '/':
path = path[1:]
return path
def compare(src, std, dst, cc):
for root, _, files in os.walk(src):
for f in files:
src_path = os.path.join(root, f)
path_fragment = dejoin(src_path, src)
std_path = os.path.join(std, path_fragment)
dst_path = os.path.join(dst, path_fragment)
copy = not os.path.exists(std_path)
if cc and not copy:
copy = not filecmp.cmp(src_path, std_path)
if copy:
print(src_path)
os.makedirs(os.path.dirname(dst_path), exist_ok=True)
shutil.copy2(src_path, dst_path)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('src', help='the source directory, whose child files might be saved')
parser.add_argument('std', help='the standard directory, which the source compared to')
parser.add_argument('dst', help='the directory to save files')
parser.add_argument('-c', '--compare-contents', help='compare file content', action='store_true', default=False)
args = parser.parse_args()
compare(args.src, args.std, args.dst, args.compare_contents)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment