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?
Shutil module offers high-level operation on a file like a copy, create, and remote operation on the file. It comes under Python’s standard utility modules. This module helps in automating the process of copying and removal of files and directories. In this article, we will learn this module.
This module helps in automating process of copying and removal of files and directories. shutil.which () method tells the path to an executable application which would be run if the given cmd was called. This method can be used to find a file on computer which is present on the PATH.
import shutil import os path = 'D:\DSCracker\DS Cracker\NewPython\python1' statistics=shutil.disk_usage (path) print (statistics) usage (total=1000203087872, used=9557639168, free=990645448704) shutil.which () function returns the path to an executable application which would run if the given command cmd was called.
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).
You need to either include the module name when you catch the exception:
except shutil.Error as err:
Or import it explicitly:
from shutil import copytree, Error
# the rest of your code...
try:
copytree(src, dest)
except Error as err:
errors.extend(err.args[0])
To view the traceback and exception information, you have a few options:
Don't catch the exception. Your script will be halted and all the error information will be printed.
If you want the script to continue, then you're really asking a duplicate of this SO question. I would reference that question; the accepted answer is written very well.
And by the way, you should avoid calling it an array. This particular exception object has a list of tuples, and arrays are an entirely different data structure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With