計算多個資料夾中,總檔案個數(python)
阿新 • • 發佈:2019-01-01
# -*- coding: utf-8 -*- # Time:2017.03.28 # Author:coplin # Function:Count the number of image file. import os def eachFile(PATH): pathDir = os.listdir(PATH) SUM=0 infolist="" for allDir in pathDir: child = os.path.join('%s%s%s' % (PATH,'/',allDir)) SUM+=len(os.listdir(child)) infolist+='\n'+allDir+':'+str(len(os.listdir(child))) print infolist,"\nallNumber:"+str(SUM) #write data to file(.txt) priceFile = file(PATH+".txt", "w+") priceFile.writelines(str(infolist)) priceFile.writelines("\nallNumber:" + str(SUM)) priceFile.close() print "writeFile success!" if __name__ == '__main__': PATH = "./data" print eachFile(PATH)