Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 16th, 2006, 02:45 PM
rbt
Guest
 
Posts: n/a
Default 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(target_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.SetFileAttributes(os.path.join(root, f),
win32con.FILE_ATTRIBUTE_NORMAL)
except:
pass

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

def remove_dirs(target_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.SetFileAttributes(os.path.join(root, d),
win32con.FILE_ATTRIBUTE_NORMAL)
except:
pass

try:
# Try to delete the directory.
os.rmdir(os.path.join(root, d))
except:
pass
  #2  
Old January 16th, 2006, 02:55 PM
Fuzzyman
Guest
 
Posts: n/a
Default 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

  #3  
Old January 16th, 2006, 02:55 PM
Tim N. van der Leeuw
Guest
 
Posts: n/a
Default Re: recursively removing files and directories

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

cheers,

--Tim

  #4  
Old January 16th, 2006, 02:55 PM
Richie Hindle
Guest
 
Posts: n/a
Default 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
  #5  
Old January 16th, 2006, 03:25 PM
rbt
Guest
 
Posts: n/a
Default 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 :)
  #6  
Old January 16th, 2006, 03:35 PM
rbt
Guest
 
Posts: n/a
Default 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]
 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles