[issue40160] documentation example of os.walk should be less destructive

New submission from John Taylor <johntaylor@gmail.com>: The example for os.walkdir should be less destructive. It currently recursively removes all files and directories. I will be submitting a PR on GitHub. ---------- assignee: docs@python components: Documentation messages: 365625 nosy: docs@python, jftuga priority: normal severity: normal status: open title: documentation example of os.walk should be less destructive versions: Python 3.8 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40160> _______________________________________

Change by Roundup Robot <devnull@psf.upfronthosting.co.za>: ---------- keywords: +patch nosy: +python-dev nosy_count: 2.0 -> 3.0 pull_requests: +18678 stage: -> patch review pull_request: https://github.com/python/cpython/pull/19313 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40160> _______________________________________

John Taylor <johntaylor@gmail.com> added the comment: https://github.com/python/cpython/pull/19313 I have just signed the CLA. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40160> _______________________________________

Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: The proposed replacement doesn't succeed in demonstrating why topdown=False is necessary. Consider doing a rename instead of a deletion or print. ---------- nosy: +rhettinger _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40160> _______________________________________

John Taylor <johntaylor@gmail.com> added the comment: I would prefer an example that does not actually modify the file system. Is there any way this could be achieved, yet still demonstrate why topdown=False is necessary? ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40160> _______________________________________

Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: One possibility is a gathering cumulative directory statistics that include totals from all descendants (i.e. how many bytes of files would you save by removing the directory with rm -rf). Outside of aggregating statistics, the normal reason to use topdown=False is when paths are being mutated. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40160> _______________________________________

Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment: I do not think there is clearer example of topdown=False than recursive remove. If you think that this example is destructive, consider how destructive is any possible example for shutil.rmtree()! ---------- nosy: +serhiy.storchaka _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40160> _______________________________________

Kyle Stanley <aeros167@gmail.com> added the comment: Serhiy Storchaka wrote:
I do not think there is clearer example of topdown=False than recursive remove.
If you think that this example is destructive, consider how destructive is any possible example for shutil.rmtree()!
I concur with Serhiy. If the example is changed to just print out the file and directory path, as the PR proposes to do, it seems to defeat the purpose of using `topdown=False` (and the code example) in the first place. If anything, I would suggest adding succinct comments or a note that very briefly explains how one could see a visual demonstration of what it does without removing any files or directories. For example: ``print(f"os.remove({os.path.join(root, name)})")``. ---------- nosy: +aeros _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40160> _______________________________________

John Taylor <johntaylor@gmail.com> added the comment: I made the suggested change to just print the os.remove() statements (instead of executing them) and also removed the 'skip news'. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40160> _______________________________________

Kyle Stanley <aeros167@gmail.com> added the comment:
I made the suggested change to just print the os.remove() statements (instead of executing them) and also removed the 'skip news'.
I think you may have misunderstood the suggestion. Specifically, the key part was "I would suggest adding succinct comments or a note that very briefly explains how one could see a visual demonstration...". This would mean the actual code in the example would be *unchanged*, but with a new code comment or separate note after the example that explains how one could replace ``os.remove(os.path.join(root, name))`` with ``print(f"os.remove({os.path.join(root, name)})")`` for a purely visual demonstration that doesn't affect any local files. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40160> _______________________________________

Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment: 1. There is already a warning before example. 2. Even if you blindly copy-paste the example it will not work. You have to set the top variable. So I don't see any problem here. You always can shoot yourself in the foot if try enough. ---------- resolution: -> not a bug stage: patch review -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40160> _______________________________________
participants (5)
-
John Taylor
-
Kyle Stanley
-
Raymond Hettinger
-
Roundup Robot
-
Serhiy Storchaka