计算多个文件夹中,总文件个数(python)

# -*- 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)
发布了19 篇原创文章 · 获赞 0 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/cike0cop/article/details/69062896