Copy folders with Python

one line to copy entire folders

18 Jul 2022

Having had issues copying large folders (100Gb+) with (Path)Finder, here is the code snippet I used to perform the task with Python (much faster too):

import shutil

shutil.copytree('/path/to/source/folder/on/network/Pictures', '/path/to/destination/folder', dirs_exist_ok=True)

dirs_exist_ok=True ensures files are overwritten when present, taking care of possible past file corruptions.

with printing progress:

from datetime import datetime
from shutil import copytree,copy2

def copy2_verbose(src, dst):
    print(f"{datetime.now().strftime('%H:%M:%S')} copying: {src}")
    copy2(src,dst)

copytree('/Volumes/Nisubz’s Public Folder/movies', '/Volumes/Videos/Movies', dirs_exist_ok=True, copy_function=copy2_verbose)

links

social