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 for deleting a full tree is sometime wrong #41204

Closed
bornet mannequin opened this issue Nov 22, 2004 · 4 comments
Closed

os.walk example for deleting a full tree is sometime wrong #41204

bornet mannequin opened this issue Nov 22, 2004 · 4 comments
Assignees
Labels
docs Documentation in the Doc dir

Comments

@bornet
Copy link
Mannequin

bornet mannequin commented Nov 22, 2004

BPO 1071087
Nosy @tim-one

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/tim-one'
closed_at = <Date 2004-11-22.16:49:53.000>
created_at = <Date 2004-11-22.16:02:29.000>
labels = ['docs']
title = 'os.walk example for deleting a full tree is sometime wrong'
updated_at = <Date 2004-11-22.16:49:53.000>
user = 'https://bugs.python.org/bornet'

bugs.python.org fields:

activity = <Date 2004-11-22.16:49:53.000>
actor = 'tim.peters'
assignee = 'tim.peters'
closed = True
closed_date = None
closer = None
components = ['Documentation']
creation = <Date 2004-11-22.16:02:29.000>
creator = 'bornet'
dependencies = []
files = []
hgrepos = []
issue_num = 1071087
keywords = []
message_count = 4.0
messages = ['23234', '23235', '23236', '23237']
nosy_count = 2.0
nosy_names = ['tim.peters', 'bornet']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue1071087'
versions = []

@bornet
Copy link
Mannequin Author

bornet mannequin commented Nov 22, 2004

On page:

http://docs.python.org/lib/os-file-dir.html

the example give:

...
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
os.remove(join(root, name))
for name in dirs:
os.rmdir(join(root, name))

This don't work if one link exist in the tree pointing
to a directory.
Assume for example you have:
/tmp/mydir/a/link which is a link to /tmp/mydir/toto

The given recipe will work until /tmp/mydir/toto is an
existing dir. At this time, link will be returned as a
directory, and doing a "os.rmdir('/tmp/mydir/a/link')"
will fail.

One solution can be:
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
os.remove(join(root, name))
for name in dirs:
try:
os.rmdir(join(root, name))
except OSError:
os.remove(join(root, name))

Another better thing is to use os.path.join() instead
of join().

@bornet bornet mannequin closed this as completed Nov 22, 2004
@bornet bornet mannequin assigned tim-one Nov 22, 2004
@bornet bornet mannequin added the docs Documentation in the Doc dir label Nov 22, 2004
@bornet bornet mannequin closed this as completed Nov 22, 2004
@bornet bornet mannequin assigned tim-one Nov 22, 2004
@bornet bornet mannequin added the docs Documentation in the Doc dir label Nov 22, 2004
@tim-one
Copy link
Member

tim-one commented Nov 22, 2004

Logged In: YES
user_id=31435

Shrug -- the point of the example is the need for
topdown=False, not to illustrate platform-dependent
headaches created by links. If this is confusing, I'd rather
leave the example alone and add words saying the example
assumes there aren't links.

WRT join(), the example is already using os.path.join(). The

from os.path import join

establishes "join" as a short name for os.path.join.

@bornet
Copy link
Mannequin Author

bornet mannequin commented Nov 22, 2004

Logged In: YES
user_id=122379

Hi,

thanks for this quick answer. I agree with you that this is
enough to add few words assuming there aren't links.

And sorry for the join. I have missing the from os.path
import join.

Good day.

@tim-one
Copy link
Member

tim-one commented Nov 22, 2004

Logged In: YES
user_id=31435

OK, I added words about links, and changed the example to
spell out os.path.walk in full (I agree it's clearer that way).
Thank you for the report!

Doc/lib/libos.tex 1.145

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 9, 2022
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
Projects
None yet
Development

No branches or pull requests

1 participant