Remove all directories using wildcard

Andreas Tawn andreas.tawn at ubisoft.com
Fri Mar 18 13:00:39 EDT 2011


> I'm new to python and I am trying to figure out how to remove all sub
> directories from a parent directory using a wildcard.  For example,
> remove all sub directory folders that contain the word "PEMA" from the
> parent directory "C:\Data".
> 
> I've trying to use os.walk with glob, but I'm not sure if this is the
> right path to take.
> 
> Thanks for any suggestions!

I think I'd do something like this (untested).

import os, shutil

startDir = r"C:\Data"

for item in os.listdir(startDir):
    folder = os.path.join(startDir, item)
    if os.path.isdir(folder) and "PEMA" in item:
        shutil.rmtree(folder)

Cheers,

Drea



More information about the Python-list mailing list