recursively removing files and directories

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rbt

    #1

    recursively removing files and directories

    What is the most efficient way to recursively remove files and directories?

    Currently, I'm using os.walk() to unlink any files present, then I call
    os.walk() again with the topdown=False option and get rid of diretories
    with rmdir. This works well, but it seems that there should be a more
    efficient way. Here are my function definitions:

    def remove_files(ta rget_dir):
    # This attempts to remove _all_ files from a directory.
    # Use with caution on directories that store temporary files.

    for root, dirs, files in os.walk(target_ dir):
    for f in files:

    try:
    # Make attributes normal so file can be deleted.
    win32api.SetFil eAttributes(os. path.join(root, f),
    win32con.FILE_A TTRIBUTE_NORMAL )
    except:
    pass

    try:
    # Try to delete the file.
    os.unlink(os.pa th.join(root, f))
    except:
    pass

    def remove_dirs(tar get_dir):
    # This attempts to remove _all_ sub directories from a directory.
    # Use with caution on directories that store temporary information.

    for root, dirs, files in os.walk(target_ dir, topdown=False):
    for d in dirs:

    try:
    # Make attributes normal so dir can be deleted.
    win32api.SetFil eAttributes(os. path.join(root, d),
    win32con.FILE_A TTRIBUTE_NORMAL )
    except:
    pass

    try:
    # Try to delete the directory.
    os.rmdir(os.pat h.join(root, d))
    except:
    pass
  • Fuzzyman

    #2
    Re: recursively removing files and directories

    shutil.rmtree

    You might need an ``onerror`` handler to sort out permissions.

    There is one for just this in pathutils :

    http://www.voidspace.org.uk/python/pathutils.html

    All the best,

    Fuzzyman
    http://www.voidspace.org.uk/python/index.shtml

    Comment

    • Tim N. van der Leeuw

      #3
      Re: recursively removing files and directories

      Wasn't this the example given in the Python manuals? Recursively
      deleting files and directories?

      cheers,

      --Tim

      Comment

      • Richie Hindle

        #4
        Re: recursively removing files and directories


        [rbt][color=blue]
        > What is the most efficient way to recursively remove files and directories?[/color]

        shutil.rmtree: http://docs.python.org/lib/module-shutil.html#l2h-2356

        --
        Richie Hindle
        richie@entrian. com

        Comment

        • rbt

          #5
          Re: recursively removing files and directories

          Tim N. van der Leeuw wrote:[color=blue]
          > Wasn't this the example given in the Python manuals? Recursively
          > deleting files and directories?[/color]

          I don't know... I wrote it without consulting anything. Hope I'm not
          infringing on a patent :)

          Comment

          • rbt

            #6
            Re: recursively removing files and directories

            Fuzzyman wrote:[color=blue]
            > shutil.rmtree[/color]

            Many thanks. I'll give that a go!
            [color=blue]
            >
            > You might need an ``onerror`` handler to sort out permissions.
            >
            > There is one for just this in pathutils :
            >
            > http://www.voidspace.org.uk/python/pathutils.html
            >
            > All the best,
            >
            > Fuzzyman
            > http://www.voidspace.org.uk/python/index.shtml
            >[/color]

            Comment

            Working...