Search code examples
pythonglobxbmcshutilkodi

Python - shutil How to select multiple directories for deletion with similar names


I have a Directory called "master" inside I have sub directories as so.

|master----
         |test.directory.one
         |test directory.two
         |test.directory.three
         |test.directory.four
         |test.directory.five
         |this.directory.keep

I would like to create a python script that deletes

all the directories that start with test.directory and ignore anything else

Thanks for you help. Any guidance is greatly appreciated.

Simontfs


Solution

  • import glob
    import shutil
    
    deldirs = glob.glob("path/to/master/test.directory.*")
    for dir_ in deldirs:
        shutil.rmtree(dir_)