Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > recursively removing files and directories

Reply
Thread Tools

recursively removing files and directories

 
 
rbt
Guest
Posts: n/a
 
      01-16-2006
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
 
Reply With Quote
 
 
 
 
Fuzzyman
Guest
Posts: n/a
 
      01-16-2006
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

 
Reply With Quote
 
 
 
 
Tim N. van der Leeuw
Guest
Posts: n/a
 
      01-16-2006
Wasn't this the example given in the Python manuals? Recursively
deleting files and directories?

cheers,

--Tim

 
Reply With Quote
 
Richie Hindle
Guest
Posts: n/a
 
      01-16-2006

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


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

--
Richie Hindle
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
rbt
Guest
Posts: n/a
 
      01-16-2006
Tim N. van der Leeuw wrote:
> Wasn't this the example given in the Python manuals? Recursively
> deleting files and directories?


I don't know... I wrote it without consulting anything. Hope I'm not
infringing on a patent
 
Reply With Quote
 
rbt
Guest
Posts: n/a
 
      01-16-2006
Fuzzyman wrote:
> shutil.rmtree


Many thanks. I'll give that a go!

>
> 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
>

 
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
recursively remove all the directories and files which begin with '.' albert kao Python 5 05-16-2010 06:33 PM
recursively removing files and directories bahoo Python 2 04-09-2007 07:17 PM
list files or directories recursively in high efficiency Ken Java 1 12-02-2005 06:49 PM
Recursively listing files into directories Alain.Feler Ruby 3 05-15-2005 06:09 PM
Help on Shell API to delete files/directories recursively Trinity C++ 1 11-07-2003 03:33 PM



Advertisments