Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

list files recursivly with python (See related posts)

   1  
   2  import os
   3  import stat
   4  
   5  def walktree (top = ".", depthfirst = True):
   6      names = os.listdir(top)
   7      if not depthfirst:
   8          yield top, names
   9      for name in names:
  10          try:
  11              st = os.lstat(os.path.join(top, name))
  12          except os.error:
  13              continue
  14          if stat.S_ISDIR(st.st_mode):
  15              for (newtop, children) in walktree (os.path.join(top, name), depthfirst):
  16                  yield newtop, children
  17      if depthfirst:
  18          yield top, names
  19  
  20  for (basepath, children) in walktree("/home",False):
  21      for child in children:
  22          print os.path.join(basepath, child)

You need to create an account or log in to post comments to this site.


Click here to browse all 5309 code snippets

Related Posts