28

如何獲得這篇文章,以遵循Python 2.6中的符號鏈接?Python os.walk +關注符號鏈接

def load_recursive(self, path): 
    for subdir, dirs, files in os.walk(path): 
     for file in files: 
      if file.endswith('.xml'): 
       file_path = os.path.join(subdir, file) 
       try: 
        do_stuff(file_path) 
       except: 
        continue 

回答

42

設置followlinksTrue。這是第四個參數os.walk方法,以下再現:在Python 2.6的溶液中加入

os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]]) 

此選項。

+5

謝謝'os.walk(path,followlinks = True):'做了竅門,儘管Python文檔對此很不明確:http://docs.python.org/library/os.path.html# os.path.walk – 2010-09-22 17:08:04

+6

@Frank:當然不清楚;你正在查看'os.path.walk'的文檔,它是一個單獨的(舊的和不贊成的)函數。您應該查看['os.walk'](http://docs.python.org/library/os.html#os.walk)文檔。 – tzot 2010-10-22 10:14:21