Completely Deleting A Directory

Discussion in 'Python' started by Lawrence D'Oliveiro, Apr 26, 2010.

  1. 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
     
    Lawrence D'Oliveiro, Apr 26, 2010
    #1
    1. Advertising

  2. Lawrence D'Oliveiro

    MrJean1 Guest

    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
     
    MrJean1, Apr 26, 2010
    #2
    1. Advertising

  3. Lawrence D'Oliveiro

    MrJean1 Guest

    The answer to 1) is no, due to topdown = False in the call to os.walk.

    /Jean

    On Apr 26, 8:31 am, MrJean1 <> 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
     
    MrJean1, Apr 26, 2010
    #3
  4. 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
     
    Patrick Maupin, Apr 26, 2010
    #4
  5. In message
    <>, 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.
     
    Lawrence D'Oliveiro, Apr 27, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sara rafiee
    Replies:
    3
    Views:
    1,083
    Scott Allen
    Oct 4, 2004
  2. Min
    Replies:
    6
    Views:
    423
    Ron Samuel Klatchko
    Jul 1, 2003
  3. Harry Barker
    Replies:
    2
    Views:
    537
    Alf P. Steinbach
    Apr 19, 2006
  4. Todd
    Replies:
    1
    Views:
    338
  5. crea
    Replies:
    2
    Views:
    428
    Nobody
    Dec 28, 2012
Loading...

Share This Page