Python統計文件夾下數量

import os
from os import path
import datetime
def getAllFile(rootPath):
    fileList = []
    files=os.listdir(rootPath) #獲取文件列表
    for fileName in files:
        fullpath=path.join(rootPath,fileName)
        if(path.isdir(fullpath)):#如果是文件夾 遞歸出所有的文件 添加到數組中
            getAllFile(fileList,fullpath)
        else:
            if str(datetime.date.today()  - datetime.timedelta(days=1)) in fullpath or str(datetime.date.today()  - datetime.timedelta(days=1)).replace('-','') in fullpath: #按需過濾自己想要識別的文件
                fileList.append(fullpath)
            else:
                pass
    return fileList
# def readFileLine(lineCount,fileArray):
#     length=len(fileArray)
#     for i in range(lineCount,length):
#         count = len(open(fileArray[i],'rb').readlines())
#         lineCount=lineCount+count
#     return lineCount
def readFileLine(lineCount,fileArray):
    length=len(fileArray)
    for i in range(lineCount,length):
        count = os.popen('cat {}|wc -l '.format(fileArray[i])).read()
        lineCount=lineCount+count
    return lineCount
def run(p):
    for i in os.listdir(p):
        print(i,readFileLine(0,getAllFile(os.path.join(p,i))))
p=r'C:\Users\mzj\Desktop\dp'
run(p)
# print( datetime.date.today()  - datetime.timedelta(days=1))

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章