zoukankan      html  css  js  c++  java
  • 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 + ' ')
        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' + ' ')

    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)

  • 相关阅读:
    Wepy 格式化和语法高亮(vscode)
    TypeError: Cannot read property '_wrapper' of undefined
    Vue 自定义事件传参
    Uncaught (in promise) undefined
    微信小程序 获取用户昵称、头像
    微信小程序scroll-view去除滚动条
    微信小程序从子页面退回父页面时的数据传递 wx.navigateBack()
    wx: wx.showModal 回调函数中调用自定义方法
    c#后端 小程序上传图片
    小程序配置,通用域名配置文件
  • 原文地址:https://www.cnblogs.com/yangwithtao/p/6940311.html
Copyright © 2011-2022 走看看