-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update rm_rf function to force remove directories #13270
Conversation
else: | ||
shutil.rmtree(str(path), onerror=onerror) | ||
"""Force remove directory even if it's not empty.""" | ||
for root, dirs, files in os.walk(path, topdown=False): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we add some tests here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first glance the key difference is ignoring errors
That's not a
for file in files: | ||
os.remove(os.path.join(root, file)) | ||
for dir in dirs: | ||
shutil.rmtree(os.path.join(root, dir), ignore_errors=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ignoring errors doesn't imply success, it's just hiding the mess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shutil.rmtree
can remove an non-empty directory recursively (that's its whole point over os.rmdir
!). So here you're doing the same thing as before, but just with more manual work and simply hiding instead of reporting errors. As already pointed out, that's not a suitable approach.
closes #13269
In previous
rm_rf
function Usesshutil.rmtree(path, onerror=onerror)
, which recursively deletes everything, handling errors via onerror.Now i update the
rm_rf
function to Explicitly walks the directory tree bottom-up(topdown=False)
and removes files first, then subdirectories before deleting the main directory.Now there are no more warnings after passing all tests.