您的位置:首页技术文章
文章详情页

Python 遍历文件夹,统计所有不同后缀的文件数量与比例

【字号: 日期:2022-08-21 15:31:08浏览:19作者:猪猪

问题描述

在Github上经常看到项目里不同语言的占比,突然想写一个试试。

Python 遍历文件夹,统计所有不同后缀的文件数量与比例

自己写完一个残缺的,感觉肯定不是最优解,所以来请教大家的写法。谢谢。

!/usr/bin/env pythoncoding:utf-8

import os

if name == ’__main__’:

work_space = os.getcwd()pycount = 0javacount = 0jscount = 0wecount = 0othercount = 0total = 0print work_spaceprint ’正在计算比例,请稍候.....’.decode(’utf-8’)for path, dir, file_arr in os.walk(’./’): for file_name in file_arr:javasuffix = file_name[-5:]pysuffix = file_name[-3:]if cmp(pysuffix, ’.py’) == 0: pycount += 1elif cmp(pysuffix, ’.js’) == 0: jscount += 1elif cmp(javasuffix, ’.java’) == 0: javacount += 1elif cmp(pysuffix, ’.we’) == 0: wecount += 1else: othercount += 1total = pycount + jscount + javacount + wecount + othercounttotal = float(total)print ’计算完成: 总文件数 : %d ’.decode(’utf-8’) % totalprint ’文件数 Java: %d js: %d py: %d we: %d others: %d ’.decode(’utf-8’) % ( javacount, jscount, pycount, wecount, othercount)print ’所占比例 Java: %.2f%% js:%.2f%% py:%.2f%% we:%.2f%% others:%.2f%%’.decode(’utf-8’) % ( javacount / total * 100, jscount / total * 100, pycount / total * 100, wecount / total * 100, othercount / total * 100)

问题解答

回答1:

#coding: utf-8import osfrom itertools import groupbyfile_lst = []for path, dir, files in os.walk(’./’): file_lst += filesfile_count = len(file_lst) * 1.0for key, lst in groupby(file_lst, key=lambda x: os.path.splitext(x)[1]): print key, round(len(list(lst))/file_count, 2)

看第二种:

#coding: utf-8import osfrom collections import defaultdictfile_count = 1.0res = defaultdict()for path, dir, files in os.walk(’./’): file_count += len(files) for file in files:suf = os.path.splitext(file)[1]res[suf] = res.setdefault(suf, 0) + 1for k, v in res.iteritems(): print k, round(v/file_count, 4) * 100回答2:

改进后的

# coding: utf-8import osfrom itertools import groupbydef getpercent(num): return str(round(num / file_count * 100, 2))def getresult(): for key in language_dict:print ’%s count:%d percent:%s%%’ % (key, language_dict.get(key), getpercent(language_dict[key]))if __name__ == ’__main__’: file_lst = [] language_dict = {} for path, dirs, files in os.walk(’./’):file_lst += files file_count = len(file_lst) * 1.0 for key, lst in groupby(file_lst, key=lambda x: os.path.splitext(x)[1]):if key not in language_dict.keys(): language_dict[key] = len(list(lst))else: language_dict[key] = language_dict.get(key) + len(list(lst)) getresult()

标签: Python 编程