python 统计文件夹各类格式文件数量, 具通用性
# -*-coding:utf-8-*-
#=============================================================================== ?
# 文件夾內
# 1、統計總文件數量 ?
# 2、統計各種格式文件數量
# 3、日志輸出
#===============================================================================?
import os,time,sys
def getPrettyTime():
??? return time.strftime('%y-%m-%d %H:%M:%S', time.localtime())
def log(content):
?? ?f = open('log.txt','aw+')
?? ?f.write('<' + getPrettyTime() + '> ')
?? ?f.write(content + '\n')
?? ?f.close()
def runStatu(Reset):
?? ?if Reset == True:
?? ??? ?print getPrettyTime(),'Begin to run <<< <<< <<<'
?? ??? ?log('Begin to run... ... ... ... ... ... ... ... ... ...',)
?? ?else:
?? ??? ?print getPrettyTime(),'Finish to run >>> >>> >>>'
?? ??? ?log('Finish to run' + '\n')
def fileInfor(path):
?? ?print 'name: ',os.path.basename(path)
?? ?print 'size: ',os.path.getsize(path)
?? ?log('name: ' + os.path.basename(path))
?? ?log('size: ' + str(os.path.getsize(path)))
?? ?formatName = {}?? ?
?? ?for root, dirs , files in os.walk(path):
?? ??? ?print 'all fileNumber: ', len(files)
?? ??? ?log('all fileNumber: ' + str(len(files)))?? ?
?? ??? ?for f in files:?? ??? ??? ?
?? ??? ??? ?if not (f.split('.')[-1] in formatName.keys()):
?? ??? ??? ??? ?formatName[f.split('.')[-1]] = 1
?? ??? ??? ?else:
?? ??? ??? ??? ?formatName[f.split('.')[-1]] = formatName[f.split('.')[-1]] + 1
?? ?for i in formatName:
?? ??? ?print '[',i,']','fileNumber: ', formatName[i]
?? ??? ?log('[' + i + ']' + 'fileNumber: ' + str(formatName[i]))
if __name__ == '__main__':
?? ?runStatu(1)
?? ?fileInfor(sys.argv[1])
??? runStatu(0)
轉載于:https://www.cnblogs.com/yangwithtao/p/6940311.html
總結
以上是生活随笔為你收集整理的python 统计文件夹各类格式文件数量, 具通用性的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CLR探索系列:Windbg+SOS动态
- 下一篇: 夏天用水代替冷却液会不会导致节温器坏?