943,772 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 457
  • Python RSS
Jul 23rd, 2009
0

Removing directories

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pythonbegginer is offline Offline
8 posts
since Jul 2009
Jul 23rd, 2009
0

Re: Removing directories

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
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Jul 23rd, 2009
0

Re: Removing directories

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
seanlynch is offline Offline
5 posts
since Nov 2008
Jul 23rd, 2009
0

Re: Removing directories

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.
Reputation Points: 16
Solved Threads: 4
Junior Poster
shoemoodoshaloo is offline Offline
135 posts
since May 2009
Jul 23rd, 2009
0

Re: Removing directories

Click to Expand / Collapse  Quote originally posted by jlm699 ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pythonbegginer is offline Offline
8 posts
since Jul 2009
Jul 23rd, 2009
0

Re: Removing directories

try shutil.rmtree(dir) instead of os.rmdir(dir)

you will have to import 'import shutil'
Reputation Points: 10
Solved Threads: 1
Newbie Poster
seanlynch is offline Offline
5 posts
since Nov 2008
Jul 23rd, 2009
0

Re: Removing directories

Click to Expand / Collapse  Quote originally posted by seanlynch ...
try shutil.rmtree(dir) instead of os.rmdir(dir)

you will have to import 'import shutil'
That did it. Thanks everybody!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pythonbegginer is offline Offline
8 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: command line boolean
Next Thread in Python Forum Timeline: Search string help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC