Removing directories

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2009
Posts: 8
Reputation: pythonbegginer is an unknown quantity at this point 
Solved Threads: 0
pythonbegginer pythonbegginer is offline Offline
Newbie Poster

Removing directories

 
0
  #1
Jul 23rd, 2009
I have a lot of directories that start with "td." for example i have:
td.10000
td.11102
td.00384
td.35454
td.32356

Is there a way in which i can tell python to remove all directories that start with "td." ? If so, how?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,054
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 265
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Removing directories

 
0
  #2
Jul 23rd, 2009
Firstly, you can use glob to get a list containing the name of each directory that starts with td like such: dirs = glob.glob(td.*) You can find the documentation about glob here.

Next, you'll want to iterate over the list that glob returns by doing something along the lines of for dir in dirs: .

Finally, to remove there exists a built-in os.rmdir(dir) which will remove the directory specified. Read up on the various operating system interfaces that the os module offers here.
Last edited by jlm699; Jul 23rd, 2009 at 2:47 pm. Reason: icode tag fix
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 4
Reputation: seanlynch is an unknown quantity at this point 
Solved Threads: 1
seanlynch seanlynch is offline Offline
Newbie Poster

Re: Removing directories

 
0
  #3
Jul 23rd, 2009
shutil.rmtree will remove non-empty directories.

shutil.rmtree(path[, ignore_errors[, onerror]])ΒΆ

Delete an entire directory tree; path must point to a directory (but not a symbolic link to a directory). If ignore_errors is true, errors resulting from failed removals will be ignored; if false or omitted, such errors are handled by calling a handler specified by onerror or, if that is omitted, they raise an exception.

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.path.islink(), os.listdir(), os.remove() or os.rmdir(). The second parameter, path, will be the path name passed to function. The third parameter, excinfo, will be the exception information return by sys.exc_info(). Exceptions raised by onerror will not be caught.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 70
Reputation: shoemoodoshaloo is an unknown quantity at this point 
Solved Threads: 2
shoemoodoshaloo shoemoodoshaloo is offline Offline
Junior Poster in Training

Re: Removing directories

 
0
  #4
Jul 23rd, 2009
If you are using bash in the linux terminal, it is simple to just remove all directories containing "td" with:

rm -r *td*

First you may want to use ls *td* to make sure there are no other directories that would be removed in this process by mistake.

I know this is a python question, but if you didn't know how to do it in bash, it is quite simple.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 8
Reputation: pythonbegginer is an unknown quantity at this point 
Solved Threads: 0
pythonbegginer pythonbegginer is offline Offline
Newbie Poster

Re: Removing directories

 
0
  #5
Jul 23rd, 2009
Originally Posted by jlm699 View Post
Firstly, you can use glob to get a list containing the name of each directory that starts with td like such: dirs = glob.glob(td.*) You can find the documentation about glob here.

Next, you'll want to iterate over the list that glob returns by doing something along the lines of for dir in dirs: .

Finally, to remove there exists a built-in os.rmdir(dir) which will remove the directory specified. Read up on the various operating system interfaces that the os module offers here.


Well when i use this code it gives me an error and tells me that one of the directories is not empty....in fact none of them are empty, i just want to get rid of them no matter what.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 4
Reputation: seanlynch is an unknown quantity at this point 
Solved Threads: 1
seanlynch seanlynch is offline Offline
Newbie Poster

Re: Removing directories

 
0
  #6
Jul 23rd, 2009
try shutil.rmtree(dir) instead of os.rmdir(dir)

you will have to import 'import shutil'
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 8
Reputation: pythonbegginer is an unknown quantity at this point 
Solved Threads: 0
pythonbegginer pythonbegginer is offline Offline
Newbie Poster

Re: Removing directories

 
0
  #7
Jul 23rd, 2009
Originally Posted by seanlynch View Post
try shutil.rmtree(dir) instead of os.rmdir(dir)

you will have to import 'import shutil'
That did it. Thanks everybody!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC