os.walk 目錄周遊
這個方法傳回的是一個三元tupple(dirpath, dirnames, filenames),
其中第一個為起始路徑,
第二個為起始路徑下的檔案夾,
第三個是起始路徑下的檔案.
dirpath是一個string,代表目錄的路徑,
dirnames是一個list,包含了dirpath下所有子目錄的名字,
filenames是一個list,包含了非目錄檔案的名字.這些名字不包含路徑資訊,如果需要得到全路徑,需要使用 os.path.join(dirpath, name).
調用方法一:
for i in os.walk(‘c:\\nant’):
for file in i[2]:
fullpath=os.path.join(i[0],file)
print (fullpath)
調用方法二:
for root,dir,files in os.walk(‘c:\\nant’):
for file in files:
fullpath=os.path.join(root,file)