As Bob Ezuba said in his answer, shutil.rmtree()
is a better way to do it. You can recreate the directory if needed.
Using glob.glob('/your/path/*')
will not find hidden files named with a leading .
. You could call glob()
multiple times, but that's getting ugly. Nor will glob()
allow you to differentiate between files and directories, making it difficult to remove subdirectories. shutil.rmtree()
will remove all files and subdirectories.
Alternatively you can rename the directory, recreate it anew, then rmtree()
the old one. This might be better if you have any processes writing to files in the directory. And it won't leave your directory in a mess if rmtree()
fails to remove some of the files, e.g. due to permissions.