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?

Recommended Answers

All 6 Replies

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.

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.

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.

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.

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

you will have to import 'import shutil'

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

you will have to import 'import shutil'

That did it. Thanks everybody!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.