How to remove a lot of folders at once using Python?
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?
You could use the glob
module to locate the directories, then use shutil.rmtree()
on each:
from glob import iglob
import shutil
for path in iglob('.../run*'):
shutil.rmtree(path)
Because you don't need to have a complete list of all matched directories, I used glob.iglob()
to yield matched paths one by one.
上一篇: cmd命令'DEL'与python中的subprocess.Popen不起作用
下一篇: 如何使用Python一次删除大量文件夹?