Created
December 21, 2021 02:41
-
-
Save jamesperrin/9c700732e3bdb17f6570ac05060670ca to your computer and use it in GitHub Desktop.
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
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