[Tutor] Delete directories recursively

John Corry john.corry at ntlworld.com
Fri Jun 16 19:55:56 CEST 2006


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

import shutil
import os

def zaps(self):


        except OSError, inst:
            print OSError
            os.chmod(inst.filename, 0666)


  -----Original Message-----
  From: tutor-bounces at python.org [mailto:tutor-bounces at python.org]On Behalf
Of Amresh Kulkarni
  Sent: 16 June 2006 16:27
  To: tutor at python.org
  Subject: [Tutor] Delete directories recursively


  I need to delete a directory and its sub directories. However all dir's,
sub dir;s and files have read only access. How do i do this efficeintly
using the os.walk command.
  I cannot run this command on the dir as it gives me an error due to the
read only attribute. Is there any other way to do this?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060616/ee8a0842/attachment.htm 

More information about the Tutor mailing list