Remove all directories using wildcard

Discussion in 'Python' started by JSkinn3, Mar 18, 2011.

  1. JSkinn3

    JSkinn3 Guest

    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!
    JSkinn3, Mar 18, 2011
    #1
    1. Advertising

  2. JSkinn3

    Tim Golden Guest

    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
    Tim Golden, Mar 18, 2011
    #2
    1. Advertising

  3. JSkinn3

    Andreas Tawn Guest

    > 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
    Andreas Tawn, Mar 18, 2011
    #3
  4. JSkinn3

    Rikishi42 Guest

    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
    Rikishi42, Mar 19, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. yangyong
    Replies:
    2
    Views:
    4,039
    Alexander Terekhov
    Jun 27, 2003
  2. Artie Gold
    Replies:
    0
    Views:
    887
    Artie Gold
    Jun 27, 2003
  3. Thomas Rademacher

    determine directories with wildcard

    Thomas Rademacher, Mar 8, 2005, in forum: Python
    Replies:
    5
    Views:
    317
    Thomas Rademacher
    Mar 8, 2005
  4. Replies:
    7
    Views:
    820
  5. Adam Petrie
    Replies:
    8
    Views:
    293
    Adam Petrie
    Oct 11, 2004
Loading...

Share This Page