Re: [Tutor] Delete directories recursively

2006-06-16 Thread Alan Gauld
> 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.

You can'ty since the reasoin the files are read-only is to prevent 
them
being over-written or deleted! You need to convert them from read-only
and then delete them. Its not too hard provided you have the relevant
access rights to change mode.

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

You can only do from Python what you can do from the OS.
If you don't have access to the files from the OS you won't have 
access
from Python - thats why access rights are there!

For more on how to do it we need to know your OS, since this is
almost certainly a job for the OS rather than Python, unless you
want to do more than just delete the files.

If you do have access rights then you can find out all the needed
steps in the OS topic in my tutor.

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Delete directories recursively

2006-06-16 Thread Amresh Kulkarni
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


Re: [Tutor] Delete directories recursively

2006-06-16 Thread Kent Johnson
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@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Delete directories recursively

2006-06-16 Thread John Corry



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()
 
Regards,
 
John.

  -Original Message-From: [EMAIL PROTECTED] 
  [mailto:[EMAIL PROTECTED]On Behalf Of Amresh 
  KulkarniSent: 16 June 2006 16:27To: 
  tutor@python.orgSubject: [Tutor] Delete directories 
  recursivelyHi,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? -- 
  ~~AMRESH~~ 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Delete directories recursively

2006-06-16 Thread Matthew White
You must change the directory and file permissions before attempting to remove 
them;
even if you are using a python script.

Take a look at os.chmod()

-mtw

On Fri, Jun 16, 2006 at 10:26:34AM -0500, Amresh Kulkarni ([EMAIL PROTECTED]) 
wrote:
> Hi,
> 
> 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?
> 
> -- 
> ~~AMRESH~~

> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Delete directories recursively

2006-06-16 Thread Kent Johnson
Amresh Kulkarni wrote:
> Hi,
> 
> 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?

Maybe I'm missing something, but I don't understand how you expect to 
delete a directory if you don't have delete permission?

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Delete directories recursively

2006-06-16 Thread Amresh Kulkarni
Hi,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?
-- ~~AMRESH~~
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor