COPYING DIRECTORY TREES
Python
#!/usr/bin/env python3
"""COPYING DIRECTORY TREES"""
import os, tempfile, shutil
temp = tempfile.gettempdir()
src = os.path.join(temp, "source")
dst = os.path.join(temp, "destination")
os.makedirs(os.path.join(src, "subdir"))
open(os.path.join(src, "file1.txt"), 'w').write("test1")
open(os.path.join(src, "subdir/file2.txt"), 'w').write("test2")
print("Copying directory tree:")
print(f" Source: {src}")
print(f" Destination: {dst}")
shutil.copytree(src, dst)
print(f" Copied {len(list(os.walk(dst)))} directories")
for root, dirs, files in os.walk(dst):
for file in files:
print(f" {os.path.join(os.path.relpath(root, dst), file)}")
shutil.rmtree(src)
shutil.rmtree(dst)