I'm learning Python 3 and trying to write a script that will copy a directory. I'm using shutil.copytree
. From the Python documentation it says:
If exception(s) occur, an Error is raised with a list of reasons.
This exception collects exceptions that are raised during a multi-file operation. For copytree(), the exception argument is a list of 3-tuples (srcname, dstname, exception).
In the example they do this:
except Error as err:
errors.extend(err.args[0])
Here is my script:
def copyDirectory(src, dest):
errors = []
try:
shutil.copytree(src, dest)
except Error as err:
errors.extend(err.args[0])
source="C:/Users/MrRobot/Desktop/Copy"
destination="C:/Users/MrRobot/Desktop/Destination"
copyDirectory(source, destination)
moveDirectory(destination,"I:/")
Questions:
How do you properly catch an exception that might occur when using
shutil.copytree
(assuming my above script is incorrect)?How then would you view the errors that occurred, would I loop through the
errors
array?