Skip to content

Instantly share code, notes, and snippets.

@sodre
Created June 7, 2019 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sodre/69ade4a914338148424dc21cf30785fd to your computer and use it in GitHub Desktop.
Save sodre/69ade4a914338148424dc21cf30785fd to your computer and use it in GitHub Desktop.
Simple python code for overlaying a source directory on top of another (existing) dir.
# A Python function for overlaying a source directory on top of a destination dir
import os
import shutil
def overlay_dir(prefix, src, dest):
abs_src = os.path.abspath(src)
abs_dest = os.path.join(prefix, dest)
for root, dirs, files in os.walk(abs_src):
dest_root=root.replace(abs_src, abs_dest)
if not os.path.exists(dest_root):
os.mkdir(dest_root)
for f in files:
src_file=os.path.join(root, f)
dest_file=os.path.join(dest_root, f)
shutil.copy2(src_file, dest_file)
if __name__ == "__main__":
prefix = "/tmp"
user_input = ("..", "tmp")
overlay_dir(prefix, *user_input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment