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

os.walk() example of an implementation of shutil.rmtree() is incomplete #126055

Closed
vwheeler63 opened this issue Oct 27, 2024 · 3 comments · Fixed by #126067
Closed

os.walk() example of an implementation of shutil.rmtree() is incomplete #126055

vwheeler63 opened this issue Oct 27, 2024 · 3 comments · Fixed by #126067
Labels
docs Documentation in the Doc dir easy

Comments

@vwheeler63
Copy link
Contributor

vwheeler63 commented Oct 27, 2024

Documentation

In this section:

https://docs.python.org/3.9/library/os.html?highlight=os%20walk

there is a code example at the bottom that claims to be "a simple implementation of shutil.rmtree()". However it is incomplete. Reason: shutil.rmtree() also removes the passed directory whereas the code shown will not. To make it complete, add this line at the end of it after the top (highest) for loop:

os.rmdir(top)

That will make the whole code example look like this:

# Delete everything reachable from the directory named in "top",
# assuming there are no symbolic links.
# CAUTION:  This is dangerous!  For example, if top == '/', it
# could delete all your disk files.
import os
for root, dirs, files in os.walk(top, topdown=False):
    for name in files:
        os.remove(os.path.join(root, name))
    for name in dirs:
        os.rmdir(os.path.join(root, name))

os.rmdir(top)

Linked PRs

@vwheeler63 vwheeler63 added the docs Documentation in the Doc dir label Oct 27, 2024
@picnixz picnixz added the easy label Oct 28, 2024
@picnixz
Copy link
Contributor

picnixz commented Oct 28, 2024

PR is welcomed!

@vwheeler63
Copy link
Contributor Author

PR is welcomed!

Wow! I am honored! Thank you for the invite! I didn't know it was open for contribution! Fix submitted in PR #126067.

vwheeler63 added a commit to vwheeler63/cpython that referenced this issue Oct 28, 2024
@picnixz
Copy link
Contributor

picnixz commented Oct 28, 2024

Anyone is free to contribute whether it is by opening issues or addressing them in a PR (or reviewing them)! You should just read the https://devguide.python.org/ and the CONTRIBUTING.rst file in the repository if you want to submit a PR in general!

willingc pushed a commit that referenced this issue Oct 30, 2024
…l `shutil.rmtree` algorithm (GH-126067)

* gh-126055:  Add omitted command (in docs [os.walk]) for code to fulfill `shutil.rmtree` algorithm.

Resolves #126055

* gh-126055:  Fix omitted code highlighting
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 30, 2024
…fulfill `shutil.rmtree` algorithm (pythonGH-126067)

* pythongh-126055:  Add omitted command (in docs [os.walk]) for code to fulfill `shutil.rmtree` algorithm.

Resolves pythonGH-126055

* pythongh-126055:  Fix omitted code highlighting
(cherry picked from commit 597d814)

Co-authored-by: Victor Wheeler <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 30, 2024
…fulfill `shutil.rmtree` algorithm (pythonGH-126067)

* pythongh-126055:  Add omitted command (in docs [os.walk]) for code to fulfill `shutil.rmtree` algorithm.

Resolves pythonGH-126055

* pythongh-126055:  Fix omitted code highlighting
(cherry picked from commit 597d814)

Co-authored-by: Victor Wheeler <[email protected]>
willingc pushed a commit that referenced this issue Oct 30, 2024
… fulfill `shutil.rmtree` algorithm (GH-126067) (GH-126200)

gh-126055:  Add omitted command (in docs [os.walk]) for code to fulfill `shutil.rmtree` algorithm (GH-126067)

* gh-126055:  Add omitted command (in docs [os.walk]) for code to fulfill `shutil.rmtree` algorithm.

Resolves GH-126055

* gh-126055:  Fix omitted code highlighting
(cherry picked from commit 597d814)

Co-authored-by: Victor Wheeler <[email protected]>
willingc pushed a commit that referenced this issue Oct 30, 2024
… fulfill `shutil.rmtree` algorithm (GH-126067) (GH-126199)

gh-126055:  Add omitted command (in docs [os.walk]) for code to fulfill `shutil.rmtree` algorithm (GH-126067)

* gh-126055:  Add omitted command (in docs [os.walk]) for code to fulfill `shutil.rmtree` algorithm.

Resolves GH-126055

* gh-126055:  Fix omitted code highlighting
(cherry picked from commit 597d814)

Co-authored-by: Victor Wheeler <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir easy
Projects
None yet
2 participants