Skip to content

Instantly share code, notes, and snippets.

@jamesperrin
Created December 21, 2021 02:41
Show Gist options
  • Save jamesperrin/9c700732e3bdb17f6570ac05060670ca to your computer and use it in GitHub Desktop.
Save jamesperrin/9c700732e3bdb17f6570ac05060670ca to your computer and use it in GitHub Desktop.
import os
import shutil
import errno
# def copyanything(src, dst):
# try:
# shutil.copytree(src, dst)
# except OSError as exc: # python >2.5
# if exc.errno == errno.ENOTDIR:
# shutil.copy(src, dst)
# else: raise
src_loc = "C:\\Path\\To\\Source\\Folder"
dst_loc = "C:\\Path\\To\\Destination\\Folder"
# copyanything(src_loc, dst_loc)
def copyrecursively(source_folder, destination_folder):
'''
Python: copy folder content recursively
https://stackoverflow.com/questions/23329048/python-copy-folder-content-recursively
'''
for root, dirs, files in os.walk(source_folder):
for item in files:
src_path = os.path.join(root, item)
dst_path = os.path.join(destination_folder, src_path.replace(source_folder, ""))
print(dst_path)
if os.path.exists(dst_path):
if os.stat(src_path).st_mtime > os.stat(dst_path).st_mtime:
shutil.copy2(src_path, dst_path)
else:
shutil.copy2(src_path, dst_path)
for item in dirs:
src_path = os.path.join(root, item)
dst_path = os.path.join(destination_folder, src_path.replace(source_folder, ""))
if not os.path.exists(dst_path):
os.mkdir(dst_path)
copyrecursively(src_loc, dst_loc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment