How to search for a folder name in python and delete it

后端 未结 2 1412
情深已故
情深已故 2021-01-24 02:39

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

2条回答
  •  不要未来只要你来
    2021-01-24 03:11

    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

提交回复
热议问题