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().
|