Python 遍历文件夹,统计所有不同后缀的文件数量与比例
迷茫
迷茫 2017-04-18 10:28:06
0
2
736

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

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

!/usr/bin/env python

coding:utf-8

import os

if name == '__main__':

work_space = os.getcwd()
pycount = 0
javacount = 0
jscount = 0
wecount = 0
othercount = 0
total = 0
print work_space
print '正在计算比例,请稍候.....'.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 += 1
        elif cmp(pysuffix, '.js') == 0:
            jscount += 1
        elif cmp(javasuffix, '.java') == 0:
            javacount += 1
        elif cmp(pysuffix, '.we') == 0:
            wecount += 1
        else:
            othercount += 1

total = pycount + jscount + javacount + wecount + othercount
total = float(total)
print '计算完成: 总文件数 : %d '.decode('utf-8') % total
print '文件数   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)
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全員に返信(2)
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!