Deleting large numbers of files in python
If you actually want to delete the whole directory tree, shutils.rmtree
should be faster than os.remove
(which is the same as os.unlink
). It also allows you to specify a callback function to handle errors.
The suggestion in the comment by @nmichaels is also good, you can os.rename
the directory then make a new one in its place and use shutils.rmtree
on the original, renamed directory.