Skip to content
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

Closed
wants to merge 2 commits into from

Conversation

ayushjariyal
Copy link

closes #13269
In previous rm_rf function Uses shutil.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.

Screenshot from 2025-03-03 01-00-05

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):
Copy link

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?

Copy link
Member

@RonnyPfannschmidt RonnyPfannschmidt left a 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)
Copy link
Member

@RonnyPfannschmidt RonnyPfannschmidt Mar 18, 2025

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.

Copy link
Member

@The-Compiler The-Compiler left a 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pytest: garbage cannot be removed [Errno 39]
4 participants