If you really want to delete the entire 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 @nmichaels comment is also good, you can os.rename create a directory and then create a new one in its place and use shutils.rmtree in the original, renamed directory.
source share