glob() that traverses a folder tree

Discussion in 'Python' started by seannakasone@yahoo.com, May 11, 2006.

  1. Guest

    i'm looking for something like glob.glob() that traverses
    sub-directories. is there anything like that? i guess i'm looking for
    something to replace the unix find command.
     
    , May 11, 2006
    #1
    1. Advertising

  2. Guest

    # i'm guessing os.walk() is the best way to traverse folder trees.

    import os, glob

    for dir, subdir, files in os.walk('.\InteropSolution'):
    for file in files:
    if glob.fnmatch.fnmatch(file,"*.dll") or
    glob.fnmatch.fnmatch(file,"*.exe"):
    print dir+file
     
    , May 11, 2006
    #2
    1. Advertising

  3. Kent Johnson Guest

    wrote:
    > # i'm guessing os.walk() is the best way to traverse folder trees.
    >
    > import os, glob
    >
    > for dir, subdir, files in os.walk('.\InteropSolution'):
    > for file in files:
    > if glob.fnmatch.fnmatch(file,"*.dll") or
    > glob.fnmatch.fnmatch(file,"*.exe"):
    > print dir+file


    Or use Jason Orendorff's path module. For a single glob it is very easy:

    import path
    for f in path.path('.\InteropSolution').walkfiles('*.dll'):
    print f

    For multiple globs you have to work a little harder:
    for f in path.path('.\InteropSolution').walkfiles():
    if f.fnmatch('*.dll') or f.fnmatch('*.exe'):
    print f

    or maybe
    for f in path.path('.\InteropSolution').walkfiles():
    if f.ext in ['.dll', '.exe']:
    print f

    http://www.jorendorff.com/articles/python/path/index.html

    Kent
     
    Kent Johnson, May 11, 2006
    #3
  4. Guest

    awesome. thanks.
     
    , May 12, 2006
    #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. Georgy Pruss
    Replies:
    15
    Views:
    726
    Tim Roberts
    Dec 1, 2003
  2. Tim Peters
    Replies:
    1
    Views:
    362
    Duncan Booth
    Dec 1, 2003
  3. Sean Berry

    Question about glob.glob <--newbie

    Sean Berry, May 4, 2004, in forum: Python
    Replies:
    3
    Views:
    348
    David M. Cooke
    May 4, 2004
  4. Elbert Lev

    glob.glob unicode bug or feature

    Elbert Lev, Jul 31, 2004, in forum: Python
    Replies:
    5
    Views:
    399
    Neil Hodgson
    Aug 2, 2004
  5. Hitesh

    glob.glob output

    Hitesh, Mar 12, 2007, in forum: Python
    Replies:
    6
    Views:
    405
    Hitesh
    Mar 13, 2007
Loading...

Share This Page