[Tutor] Delete directories recursively
Amresh Kulkarni
amresh.kulkarni at gmail.com
Fri Jun 16 20:56:17 CEST 2006
Thanks guys,
Error handling seems to be a nice idea to approach this problem. i checked
Kent's code and it works fine.
I was using a more crude method.
def removeDir(dirName) :
#Remove any read-only permissions on file.
removePermissions(dirName)
for name in os.listdir(dirName):
file = os.path.join(dirName, name)
if not os.path.islink(file) and os.path.isdir(file):
removeDir(file)
else:
removePermissions(file)
os.remove(file)
os.rmdir(dirName)
return
def removePermissions(filePath) :
#if (os.access(filePath, os.F_OK)) : #If path exists
if (not os.access(filePath, os.W_OK)) :
os.chmod(filePath, 0666)
return
however shutil seems to be more simple and efficient here!
Regards,
Amresh
On 6/16/06, Kent Johnson <kent37 at tds.net> wrote:
>
> John Corry wrote:
> >
> > Amresh,
> >
> > I had this problem a few months back. I approached it backwards. Maybe
> > not the right way to do it. I removed all the files and directories and
> > then had my exception handle the file if it was read only. The
> > exception handler changes the file from read-only to not read only and
> > then calls the function again.
> >
> > Is there a better way to do it? Would appreciate feedback on the code
> > below.
> >
> > import shutil
> > import os
> >
> > def zaps(self):
> >
> > try:
> > shutil.rmtree('f:/m2m')
> >
> >
> > except OSError, inst:
> > print OSError
> > os.chmod(inst.filename, 0666)
> > self.zaps()
>
> I imagine this could be expensive if you have a deep directory hierarchy
> with lots of read-only files - you have to start the traversal from
> scratch each time you get an error. If you have more than 1000 read-only
> files you will get a stack overflow from the recursion.
>
> shutil.rmtree() actually takes an optional error handler argument.
> According to the docs, "If onerror is provided, it must be a callable
> that accepts three parameters: function, path, and excinfo. The first
> parameter, function, is the function which raised the exception; it will
> be os.listdir(), os.remove() or os.rmdir()."
>
> So something like this should work and be faster because the directory
> traversal doesn't restart each time (UNTESTED!!):
>
> def handle_error(fn, path, excinfo):
> if fn is os.rmdir:
> # handle readonly dir
> os.chmod(path, 0666) # ?? not sure if this is correct for a dir
> os.rmdir(path) # try again
> elif fn is os.remove:
> os.chmod(path, 0666)
> os.remove(path)
>
> shutil.rmtree(top, onerror=handle_error)
>
> Kent
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
--
~~AMRESH~~
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060616/2a888e96/attachment.htm
More information about the Tutor
mailing list