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) returndef removePermissions(filePath) : #if (os.access(filePath, os.F_OK)) : #If path exists if (not
os.access(filePath, os.W_OK)) : os.chmod(filePath, 0666) returnhowever shutil seems to be more simple and efficient here!Regards,Amresh On 6/16/06,
Kent Johnson <[EMAIL PROTECTED]> 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 hierarchywith 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-onlyfiles 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 firstparameter, function, is the function which raised the exception; it willbe 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, >Kent___
Tutor maillist - Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor
-- ~~AMRESH~~
___
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor