Posted By

manatlan on 09/09/09


Tagged

file python


Versions (?)

recursive dir


 / Published in: Python
 

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

Report this snippet  

You need to login to post a comment.