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.