python 遍歷一個資料夾下所有檔名稱
阿新 • • 發佈:2019-01-28
用python遍歷一個資料夾下所有檔名稱
可以用字典的方式,程式可參考程式“E:\untitled\weather\extact_copy.py” #coding=utf-8 import os import os.path rootdir = "E:\\weather_data\\pressure\\data_before" # 指明被遍歷的資料夾 for parent,dirnames,filenames in os.walk(rootdir): #三個引數:分別返回1.父目錄 2.所有資料夾名字(不含路徑) 3.所有檔名字 for dirname in dirnames: #輸出資料夾資訊 print "parent is:" + parent print "dirname is" + dirname for filename in filenames: #輸出檔案資訊 print "parent is:" + parent print "filename is:" + filename print "the full name of the file is:" + os.path.join(parent,filename) #輸出檔案路徑資訊
注意:要想python自動讀取資料夾下所有檔案內容,可以藉助字典