Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Remove all directories using wildcard

Reply
Thread Tools

Remove all directories using wildcard

 
 
JSkinn3
Guest
Posts: n/a
 
      03-18-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!
 
Reply With Quote
 
 
 
 
Tim Golden
Guest
Posts: n/a
 
      03-18-2011
On 18/03/2011 16:41, JSkinn3 wrote:
> 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
 
Reply With Quote
 
 
 
 
Andreas Tawn
Guest
Posts: n/a
 
      03-18-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
 
Reply With Quote
 
Rikishi42
Guest
Posts: n/a
 
      03-19-2011
On 2011-03-18, JSkinn3 <> wrote:
> 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).



--
When in doubt, use brute force.
-- Ken Thompson
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wildcard String Comparisons: Set Pattern to a Wildcard Source chaoticcranium@gmail.com Python 7 10-05-2010 09:26 PM
determine directories with wildcard Thomas Rademacher Python 5 03-08-2005 02:05 PM
Getting all directories/files from current directory and using -d flag for the directories Adam Petrie Perl Misc 8 10-11-2004 01:28 PM
[OT]Re: remove or _unlink using wildcard? Artie Gold C++ 0 06-27-2003 04:31 PM
Re: remove or _unlink using wildcard? yangyong C++ 2 06-27-2003 02:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57