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)