Thanks guys,<br><br>Error handling seems to be a nice idea to approach this problem. i checked Kent's code and it works fine.<br><br>I was using a more crude method.<br><br>def removeDir(dirName) :<br> #Remove any read-only permissions on file.
<br> removePermissions(dirName)<br> for name in os.listdir(dirName):<br> file = os.path.join(dirName, name)<br> if not os.path.islink(file) and os.path.isdir(file):<br> removeDir(file)<br> else:
<br> removePermissions(file)<br> os.remove(file)<br> os.rmdir(dirName)<br> return<br>def removePermissions(filePath) :<br> #if (os.access(filePath, os.F_OK)) : #If path exists<br> if (not
os.access(filePath, os.W_OK)) :<br> os.chmod(filePath, 0666)<br> return<br><br>however shutil seems to be more simple and efficient here!<br><br>Regards,<br><br>Amresh <br><br><div><span class="gmail_quote">On 6/16/06,
<b class="gmail_sendername">Kent Johnson</b> <<a href="mailto:kent37@tds.net">kent37@tds.net</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
John Corry wrote:<br>><br>> Amresh,<br>><br>> I had this problem a few months back. I approached it backwards. Maybe<br>> not the right way to do it. I removed all the files and directories and<br>> then had my exception handle the file if it was read only. The
<br>> exception handler changes the file from read-only to not read only and<br>> then calls the function again.<br>><br>> Is there a better way to do it? Would appreciate feedback on the code<br>> below.
<br>><br>> import shutil<br>> import os<br>><br>> def zaps(self):<br>><br>> try:<br>> shutil.rmtree('f:/m2m')<br>><br>><br>> except OSError, inst:<br>> print OSError
<br>> os.chmod(inst.filename, 0666)<br>> self.zaps()<br><br>I imagine this could be expensive if you have a deep directory hierarchy<br>with lots of read-only files - you have to start the traversal from
<br>scratch each time you get an error. If you have more than 1000 read-only<br>files you will get a stack overflow from the recursion.<br><br>shutil.rmtree() actually takes an optional error handler argument.<br>According to the docs, "If onerror is provided, it must be a callable
<br>that accepts three parameters: function, path, and excinfo. The first<br>parameter, function, is the function which raised the exception; it will<br>be os.listdir(), os.remove() or os.rmdir()."<br><br>So something like this should work and be faster because the directory
<br>traversal doesn't restart each time (UNTESTED!!):<br><br>def handle_error(fn, path, excinfo):<br> if fn is os.rmdir:<br> # handle readonly dir<br> os.chmod(path, 0666) # ?? not sure if this is correct for a dir
<br> os.rmdir(path) # try again<br> elif fn is os.remove:<br> os.chmod(path, 0666)<br> os.remove(path)<br><br>shutil.rmtree(top, onerror=handle_error)<br><br>Kent<br><br>_______________________________________________
<br>Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a><br></blockquote></div><br><br clear="all">
<br>-- <br>~~AMRESH~~