Skip to content

Instantly share code, notes, and snippets.

@void32
Last active May 16, 2019 11:30
Show Gist options
  • Save void32/765dce54085e621e19916454d6a1869f to your computer and use it in GitHub Desktop.
Save void32/765dce54085e621e19916454d6a1869f to your computer and use it in GitHub Desktop.
Copy from src to dst, with option to make make parent folders and overwrite dst
def copy_smart(src, dst, make_parents=True, overwrite=False, dry_run=False):
""" Copy from src to dst, with option to make make parent folders and overwrite dst """
if not os.path(dst):
if os.path.exists(dst):
if not overwrite:
return False
if make_parents:
# Make parent directories as needed
local_dir_path = os.path.dirname(dst)
if dry_run:
print('mkdir -p %s' % local_dir_path)
else:
mkdir_p(local_dir_path)
if dry_run:
print('cp -r %s %s' % (src, dst))
else:
if os.path.isdir(src):
if os.path.isdir(dst):
dst = os.path.join(dst, os.path.basename(src))
shutil.copytree(src, dst)
else:
shutil.copy2(src, dst)
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment