This is how I copy the folder src
from source to workspace destination – replacing the content if already existing:
def copy_and_replace(directory):
target = os.path.join('workspace', directory, 'src')
source = os.path.join('..', 'source', directory, 'src')
try:
if os.path.isdir(target):
shutil.rmtree(target)
print(f"Directory removed successfully: {target}")
shutil.copytree(source, target)
print(f"Directory copied successfully: {target}")
except OSError as o:
print(f"Error, {o.strerror}: {directory}")
Now I would like to define two more folders, which should also be copied if existing. Of course I could do this like this:
def copy_and_replace(directory):
targetSrc = os.path.join('workspace', directory, 'src')
sourceSrc = os.path.join('..', 'source', directory, 'src')
targetPbl = os.path.join('workspace', directory, 'public')
sourcePbl = os.path.join('..', 'source', directory, 'public')
try:
if os.path.isdir(targetSrc): # src folder is always existing
shutil.rmtree(targetSrc)
print(f"Directory removed successfully: {targetSrc}")
shutil.copytree(sourceSrc, targetSrc)
print(f"Directory copied successfully: {targetSrc}")
if os.path.isdir(sourcePbl): # Check if public folder is existing in source
if os.path.isdir(targetPbl):
shutil.rmtree(targetPbl)
print(f"Directory removed successfully: {targetPbl}")
shutil.copytree(sourcePbl, targetPbl)
print(f"Directory copied successfully: {targetPbl}")
# do this last part for multiple folder like `specs`, `assets` and so on
except OSError as o:
print(f"Error, {o.strerror}: {directory}")
Is it possible to use a list of folder names and iterate over those items to check of folder is existing in source, and if it is exsting to copy it to target workspace?