Re: [Tutor] removedirs ?

2004-12-16 Thread Jason Child
is this what you want to do?
import os
from os.path import join
# Delete everything reachable from the directory named in 'top'.
# CAUTION: This is dangerous! For example, if top == '/', it
# could delete all your disk files.
for root, dirs, files in os.walk(top, topdown=False):
   for name in files:
   os.remove(join(root, name))
   for name in dirs:
os.rmdir(join(root, name))
I cant take credit for the code, as I found it when I was trying to hack 
together a recirsive file remover script. It was in the help files for 
the os module, under walk(). Go figure; when you try to do things the 
hard way python keeps it simple!

Peace
Jason
Ertl, John wrote:
Jason,
I could...That is the exact feature I am trying to replicate, but I would
just like to do it in Python if I can (in a simple way).  I am writing this
code in Python to avoid some funny scripting that I would need to do. To go
back to combing shell and Python again would be a bit deflating...but the
straight forward path might be the best.
Thanks,
John Ertl 

-Original Message-
From: Jason Child [mailto:[EMAIL PROTECTED]
Sent: Thursday, December 16, 2004 12:36
Cc: [EMAIL PROTECTED]
Subject: Re: [Tutor] removedirs ?
Ertl, John wrote:
 

I am trying to remove a directory that has other directories and files in
it.  I thought removedirs was supposed to do a recursive remove of files
   

and
 

directories.
When I try it I get

   

os.removedirs(DAF)
 

 

Traceback (most recent call last):
File pyshell#11, line 1, in -toplevel-
  os.removedirs(DAF)
File /home/ertlj/ertljVersion/lib/python2.3/os.py, line 167, in
removedirs
  rmdir(name)
OSError: [Errno 17] File exists: 'DAF'
Thanks,
John Ertl
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor

   

it seems to me that if its on a *nix box you could use the shell command
rm -rf target
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor
 

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] removedirs ?

2004-12-16 Thread Juan Shen
This is an iterative method, although much slower than shutil.rmtree(),  
which can be useful sometimes, while some stdout/in is added:

#!/usr/bin/python
#Filename: myrmtree.py
import os,sys,shutil
def rmtree(top):
   uncleandir=[]
   for (root,dirs,files) in os.walk(top,topdown=False):
   for name in dirs+files:
   item=os.path.join(root,name)
   if item in uncleandir:
   uncleandir.append(root)
   continue
   print 'Delete %s : (Yes or No or All or Cancel)' %(item,),
   v=raw_input()
   if v=='Y' or v=='y':
   (os.path.isfile(item) and os.remove or os.rmdir)(item)
   elif v=='N' or v=='n':
   uncleandir.append(root)
   continue
   elif v=='A' or v=='a':
   shutil.rmtree(top)
   return 1
   elif v=='C' or v=='c':
   return 2
   else:
   print 'Take your input as No.'
   continue
   return 0
from myrmtree import tree
top='/home/juan/test' #Your dir need to be removed here
rmtree(top)
Just try it and give suggestions and comments please, Hehe
   Juan Shen
Jason Child wrote:
is this what you want to do?
import os
from os.path import join
# Delete everything reachable from the directory named in 'top'.
# CAUTION: This is dangerous! For example, if top == '/', it
# could delete all your disk files.
for root, dirs, files in os.walk(top, topdown=False):
   for name in files:
   os.remove(join(root, name))
   for name in dirs:
os.rmdir(join(root, name))
I cant take credit for the code, as I found it when I was trying to 
hack together a recirsive file remover script. It was in the help 
files for the os module, under walk(). Go figure; when you try to do 
things the hard way python keeps it simple!

Peace
Jason
Ertl, John wrote:
Jason,
I could...That is the exact feature I am trying to replicate, but I 
would
just like to do it in Python if I can (in a simple way).  I am 
writing this
code in Python to avoid some funny scripting that I would need to do. 
To go
back to combing shell and Python again would be a bit deflating...but 
the
straight forward path might be the best.

Thanks,
John Ertl
-Original Message-
From: Jason Child [mailto:[EMAIL PROTECTED]
Sent: Thursday, December 16, 2004 12:36
Cc: [EMAIL PROTECTED]
Subject: Re: [Tutor] removedirs ?
Ertl, John wrote:
 

I am trying to remove a directory that has other directories and 
files in
it.  I thought removedirs was supposed to do a recursive remove of 
files
  
and
 

directories.
When I try it I get

  

os.removedirs(DAF)



Traceback (most recent call last):
File pyshell#11, line 1, in -toplevel-
  os.removedirs(DAF)
File /home/ertlj/ertljVersion/lib/python2.3/os.py, line 167, in
removedirs
  rmdir(name)
OSError: [Errno 17] File exists: 'DAF'
Thanks,
John Ertl
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor

  
it seems to me that if its on a *nix box you could use the shell command
rm -rf target
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor
 

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


RE: [Tutor] removedirs ?

2004-12-16 Thread Ertl, John
Jason,

I could...That is the exact feature I am trying to replicate, but I would
just like to do it in Python if I can (in a simple way).  I am writing this
code in Python to avoid some funny scripting that I would need to do. To go
back to combing shell and Python again would be a bit deflating...but the
straight forward path might be the best.

Thanks,

John Ertl 


-Original Message-
From: Jason Child [mailto:[EMAIL PROTECTED]
Sent: Thursday, December 16, 2004 12:36
Cc: [EMAIL PROTECTED]
Subject: Re: [Tutor] removedirs ?

Ertl, John wrote:

I am trying to remove a directory that has other directories and files in
it.  I thought removedirs was supposed to do a recursive remove of files
and
directories.

When I try it I get

 

os.removedirs(DAF)
   


Traceback (most recent call last):
  File pyshell#11, line 1, in -toplevel-
os.removedirs(DAF)
  File /home/ertlj/ertljVersion/lib/python2.3/os.py, line 167, in
removedirs
rmdir(name)
OSError: [Errno 17] File exists: 'DAF'

Thanks,

John Ertl


___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor

 

it seems to me that if its on a *nix box you could use the shell command
rm -rf target
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] removedirs ?

2004-12-16 Thread Kent Johnson
You misunderstand what removedirs does. It removes an empty directory; then, if the removed 
directory's parent is now empty, it removes that, too, and so on until it finds a directory with 
something in it or gets up to the root.

Try shutil.rmtree() or this recipe:
http://aspn.activestate.com/ASPN/docs/ActivePython/2.2/PyWin32/Recursive_directory_deletes_and_special_files.html
Kent
Ertl, John wrote:
I am trying to remove a directory that has other directories and files in
it.  I thought removedirs was supposed to do a recursive remove of files and
directories.
When I try it I get 


os.removedirs(DAF)

Traceback (most recent call last):
  File pyshell#11, line 1, in -toplevel-
os.removedirs(DAF)
  File /home/ertlj/ertljVersion/lib/python2.3/os.py, line 167, in
removedirs
rmdir(name)
OSError: [Errno 17] File exists: 'DAF'
Thanks,
John Ertl
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor