glob() that traverses a folder tree

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • seannakasone@yahoo.com

    #1

    glob() that traverses a folder tree

    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.

  • seannakasone@yahoo.com

    #2
    Re: glob() that traverses a folder tree

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

    import os, glob

    for dir, subdir, files in os.walk('.\Inte ropSolution'):
    for file in files:
    if glob.fnmatch.fn match(file,"*.d ll") or
    glob.fnmatch.fn match(file,"*.e xe"):
    print dir+file

    Comment

    • Kent Johnson

      #3
      Re: glob() that traverses a folder tree

      seannakasone@ya hoo.com wrote:[color=blue]
      > # i'm guessing os.walk() is the best way to traverse folder trees.
      >
      > import os, glob
      >
      > for dir, subdir, files in os.walk('.\Inte ropSolution'):
      > for file in files:
      > if glob.fnmatch.fn match(file,"*.d ll") or
      > glob.fnmatch.fn match(file,"*.e xe"):
      > print dir+file[/color]

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

      import path
      for f in path.path('.\In teropSolution') .walkfiles('*.d ll'):
      print f

      For multiple globs you have to work a little harder:
      for f in path.path('.\In teropSolution') .walkfiles():
      if f.fnmatch('*.dl l') or f.fnmatch('*.ex e'):
      print f

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

      http://www.jorendorff.com/articles/p...ath/index.html

      Kent

      Comment

      • seannakasone@yahoo.com

        #4
        Re: glob() that traverses a folder tree

        awesome. thanks.

        Comment

        Working...