Reputation: 33
I'm just wondering what's the behavior for python2.7 shutil's copytree function when it encounters a file error, either let it being file unreadable, file corrupted or permission problem. I checked the documentation and its not specifying how it will behave other than raising an error. But I need to know after raising that error (and assuming it was caught), will it continue with the remaining files? If not, is there a way to specify the function so that it will behave that way?
I know I could just use copy2
recursively but I really don't want to reinvent the wheel unless necessary.
Upvotes: 0
Views: 829
Reputation: 280953
If shutil.copytree
encounters an exception, it catches the exception, records it, and keeps going. At the end, it raises a shutil.Error
with a list of problem conditions that occurred.
This is not well documented, but it is visible in the source. It is also somewhat implied by the documentation line "If exception(s) occur, an Error is raised with a list of reasons.", which implies that the function can encounter multiple exceptions.
As for shutil.copy2
, that only copies one file, and there's no way it could reasonably continue if copying that file fails. If it can't copy a file, it raises an error. The file may be left half-copied if this happens.
Upvotes: 1