The documentation of shutil.copytree
states:
If exception(s) occur, an Error is raised with a list of reasons.
And the link refers to:
exception shutil.Error 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).
What is this shutil.Error
exception? I tried to catch this type of exception:
try:
shutil.copytree(source, dest) # this throws a FileExistsError for example
except shutil.Error:
# error handling
But this kind of error wasn't raised..
The documentation first states
dirs_exist_ok
dictates whether to raise an exception in case dst or any missing parent directory already exists.
Unless you specify dirs_exist_ok=True
a FileExistsError
is raised. You're right that later on the documentation states that (presumably in other cases) a shutil.Error
may be raised.
In the end I think it's a matter of interpretation, but if the code raises a FileExistsError
you should catch a FileExistsError
:
try:
shutil.copytree(source, dest)
except FileExistsError:
# error handling
>>> shutil.copytree("ws", "workshop")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/robin/.pyenv/versions/3.8.6/lib/python3.8/shutil.py", line 554, in copytree
return _copytree(entries=entries, src=src, dst=dst, symlinks=symlinks,
File "/home/robin/.pyenv/versions/3.8.6/lib/python3.8/shutil.py", line 455, in _copytree
os.makedirs(dst, exist_ok=dirs_exist_ok)
File "/home/robin/.pyenv/versions/3.8.6/lib/python3.8/os.py", line 223, in makedirs
mkdir(name, mode)
FileExistsError: [Errno 17] File exists: 'workshop'
>>> shutil.copytree("ws", "workshop", dirs_exist_ok=True)
'workshop'
The part of shutil.Error
that you quoted could be interpreted as:
This exception collects exceptions that are raised during a multi-file operation. For copytree(), [ when a
shutil.Error
is raised ] the exception argument is a list of 3-tuples (srcname, dstname, exception).
It does not mean that all exceptions during copytree()
will be of type shutil.Error
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.