今天想運行一個以前寫的Python 腳本, 解釋器是Python 3.7的, 發現os.path.walk 沒了。 在Python 2.7 下, os.path.walk 用來遍歷一個目錄, 並調用一個回調函數, 用來做需要的事情:
def recurse_dir(self):
"recurse the directory, finding the largest file"
#import os
os.path.walk(self.directory,self.get_sizes,None)
這裏使用回調函數, 還要傳一個參數列表給它。
在Python 3 裏, os.walk 取代了os.path.walk, 而且不使用回調函數:
import os
from os.path import join
for root, dirs, files in os.walk('.'):
for name in files:
print(join(root,name))
我覺得這樣寫出的代碼更清晰。