I have a directory of folders and subfolders that I need to search through to find a certain folder name, \"old data\", so that I can delete the files within \"old data\" and de
following code is for linux environment but shutil.rmtree()
is what you need
1 import os
2 import shutil
3
4 root = "/home/user/input"
5 for i in os.walk(root, topdown=False):
6 if i[0].split('/')[-1] == "test3":
7 shutil.rmtree(i[0])
os.remove()
- to remove file
os.rmdir()
- to remove empty dir
shutil.rmtree()
- to remove dir and all its content
See if the below code works for your scenario:
import os
import shutil
for root, subdirs, files in os.walk('C:/directory'):
for d in subdirs:
if d == "old data":
shutil.rmtree(os.path.join(root, d))
shutil.rmtree will delete the entire "old data" directory