Remove all directories using wildcard

J

JSkinn3

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!
 
T

Tim Golden

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.

You've got a few options. And it depends whether you just want
to get it done as simply as possible or whether you're using
this as a learning exercise.

Assuming it's somewhere between the two then you're on the right
track with os.walk:

<code - untested>
import os
import shutil

for dirpath, dirnames, filenames in os.walk ("c:/data"):
for dirname in dirnames:
if "pema" in dirname.lower ():
pema_path = os.path.join (dirpath, dirname)
print "Removing", pema_path
shutil.rmtree (pema_path)
dirnames.remove (dirname)

</code>

The key is that os.walk is designed so that the dirnames
list can be mutated in place to remove directories which
are to be ignored for whatever reason. In this case, you
delete the directory tree and remove it from os.walk's
attention.

BTW the use of os.walk here is only necessary if you need to
remove "*pema*" directories at any level under c:\data. If
you only need those immediately under c:\data then you can
just use glob.glob in union with shutil.rmtree

TJG
 
A

Andreas Tawn

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
 
R

Rikishi42

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.

I see you've got 2 suggestions allready.
Wichever you use, please note you need to begin the search from the bottom
of the tree. The call to os.walk should include a False value for the
direction of the walk:

os.walk("c:/data", False).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top