python 统计文件夹各类格式文件数量, 具通用性
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)
以上是关于python 统计文件夹各类格式文件数量, 具通用性的主要内容,如果未能解决你的问题,请参考以下文章