Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Completely Deleting A Directory

Reply
Thread Tools

Completely Deleting A Directory

 
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      04-26-2010
It doesn’t seem to mention in the documentation for os.walk
<http://docs.python.org/library/os.html> that symlinks to directories are
returned in the list of directories, not the list of files. This will lead
to an error in the os.rmdir call in the example directory-deletion routine
on that page.

This version fixes that problem.

def delete_dir(dir) :
"""deletes dir and all its contents."""
if os.path.isdir(dir) :
for parent, dirs, files in os.walk(dir, topdown = False) :
for item in files :
os.remove(os.path.join(parent, item))
#end for
for item in dirs :
item = os.path.join(parent, item)
(os.rmdir, os.remove)[os.path.islink(item)](item)
#end for
#end for
os.rmdir(dir)
#end if
#end delete_dir

 
Reply With Quote
 
 
 
 
MrJean1
Guest
Posts: n/a
 
      04-26-2010
Two comments:

1) Should delete_dir not be called instead of os.rmdir in this line

(os.rmdir, os.remove)[os.path.islink(item)](item)

2) Function rmtree in the shutil module considers symlinks to a
directory an error <http://docs.python.org/library/shutil.html#module-
shutil> since Python 2.6.

/Jean


On Apr 26, 2:09*am, Lawrence D'Oliveiro <l...@geek-
central.gen.new_zealand> wrote:
> It doesn’t seem to mention in the documentation for os.walk
> <http://docs.python.org/library/os.html> that symlinks to directories are
> returned in the list of directories, not the list of files. This will lead
> to an error in the os.rmdir call in the example directory-deletion routine
> on that page.
>
> This version fixes that problem.
>
> def delete_dir(dir) :
> * * """deletes dir and all its contents."""
> * * if os.path.isdir(dir) :
> * * * * for parent, dirs, files in os.walk(dir, topdown = False) :
> * * * * * * for item in files :
> * * * * * * * * os.remove(os.path.join(parent, item))
> * * * * * * #end for
> * * * * * * for item in dirs :
> * * * * * * * * item = os.path.join(parent, item)
> * * * * * * * * (os.rmdir, os.remove)[os.path.islink(item)](item)
> * * * * * * #end for
> * * * * #end for
> * * * * os.rmdir(dir)
> * * #end if
> #end delete_dir


 
Reply With Quote
 
 
 
 
MrJean1
Guest
Posts: n/a
 
      04-26-2010
The answer to 1) is no, due to topdown = False in the call to os.walk.

/Jean

On Apr 26, 8:31*am, MrJean1 <mrje...@gmail.com> wrote:
> Two comments:
>
> 1) Should delete_dir not be called instead of os.rmdir in this line
>
> * * * * * * * * (os.rmdir, os.remove)[os.path.islink(item)](item)
>
> 2) Function rmtree in the shutil module considers symlinks to a
> directory an error <http://docs.python.org/library/shutil.html#module-
> shutil> since Python 2.6.
>
> /Jean
>
> On Apr 26, 2:09*am, Lawrence D'Oliveiro <l...@geek-
>
>
>
> central.gen.new_zealand> wrote:
> > It doesn’t seem to mention in the documentation for os.walk
> > <http://docs.python.org/library/os.html> that symlinks to directories are
> > returned in the list of directories, not the list of files. This will lead
> > to an error in the os.rmdir call in the example directory-deletion routine
> > on that page.

>
> > This version fixes that problem.

>
> > def delete_dir(dir) :
> > * * """deletes dir and all its contents."""
> > * * if os.path.isdir(dir) :
> > * * * * for parent, dirs, files in os.walk(dir, topdown = False) :
> > * * * * * * for item in files :
> > * * * * * * * * os.remove(os.path.join(parent, item))
> > * * * * * * #end for
> > * * * * * * for item in dirs :
> > * * * * * * * * item = os.path.join(parent, item)
> > * * * * * * * * (os.rmdir, os.remove)[os.path.islink(item)](item)
> > * * * * * * #end for
> > * * * * #end for
> > * * * * os.rmdir(dir)
> > * * #end if
> > #end delete_dir


 
Reply With Quote
 
Patrick Maupin
Guest
Posts: n/a
 
      04-26-2010
On Apr 26, 4:09*am, Lawrence D'Oliveiro <l...@geek-
central.gen.new_zealand> wrote:
> It doesn’t seem to mention in the documentation for os.walk
> <http://docs.python.org/library/os.html> that symlinks to directories are
> returned in the list of directories, not the list of files. This will lead
> to an error in the os.rmdir call in the example directory-deletion routine
> on that page.


They should probably remove that example, and just point the user to
shutil.rmtree() (as they do under the os.rmdir() description).

Regards,
Pat
 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      04-26-2010
In message
<86bb4820-ab5a-49cc-9e64->, MrJean1
wrote:

> 2) Function rmtree in the shutil module considers symlinks to a
> directory an error
> <http://docs.python.org/library/shutil.html#module-shutil> since Python
> 2.6.


I don’t think that applies to subdirectories. It would be stupid if it did.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: VOIPfone down completely ... I mean very completely !! cjd UK VOIP 2 10-10-2008 01:17 PM
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
Deleting a File from Hardrive and Deleting a SubKey in Registry Harry Barker C++ 2 04-19-2006 09:34 AM
FYI: Webcontrols completely from a virtual directory (including the webctrl_client folder location) Todd ASP .Net Web Controls 1 11-21-2003 12:45 AM
Completely Deleting Pointers Min C++ 6 07-01-2003 12:38 AM



Advertisments