I have seen a lot of questions (Delete Folder Contents in Python, How to delete a file or folder?, How do I remove/delete a folder that is not empty with Python?) asking how to delete a folder (empty or not) but I haven't seen any asking about how to delete a large number of folders at once.
I tried using shutils
and writing something as shutils.rmtree('.../run*')
(all the folders that I want to delete are called run0000, run0001 and so on) but this doesn't work because * is not understood.
I finally ended up importing subprocess and using subprocess.Popen('rm -r ./run*/', shell=True)
which works because of the shell=True
but I would like to avoid this due to the security related hazards that discourage the use of shell=True
.
What is the best way to erase a large number of folders (non-empty) at once? I think that it must be to adapt some of the answers given in one of the linked questions but I haven't been able so far. How could I do it?