频道栏目
首页 > 程序开发 > Web开发 > Python > 正文
统计文件夹内.c .cpp .py类型文件的代码行数
2013-01-15 15:47:19           
收藏   我要投稿
[python]  

#_*_coding:utf_8_  

import os  

import glob  

  

def countFileLines(filename):  

    count = 0  

    try:  

        handle = open(filename, 'r')  

        for eachline in handle:  

            count += 1  

    except IOError, e:  

        print 'file open error', e  

    print 'file:' , filename, 'has %d lines' % count  

    return count  

  

def countFolderCppFilesLines(folderpath):  

    count = 0  

    filepath = str(folderpath) + '/*.cpp'  

    filelist = []  

    for file in glob.glob(filepath):  

        count += countFileLines(file)  

    print 'cpp files in folderpath: ', folderpath, ' has code %d lines' % count  

    return count  

  

def countFolderCFilesLines(folderpath):  

    count = 0  

    filepath = str(folderpath) + '/*.c'  

    filelist = []  

    for file in glob.glob(filepath):  

        count += countFileLines(file)  

    print 'C files in folderpath: ', folderpath, ' has code %d lines' % count  

    return count  

  

def countFolderPyFilesLines(folderpath):  

    count = 0  

    filepath = str(folderpath) + '/*.py'  

    filelist = []  

    for file in glob.glob(filepath):  

        count += countFileLines(file)  

    print 'python files in folderpath: ', folderpath, ' has code %d lines' % count  

    return count  

  

def folderFilesLines(folderpath):  

    count = 0  

    count += countFolderCppFilesLines(folderpath)  

    count += countFolderCFilesLines(folderpath)  

    count += countFolderPyFilesLines(folderpath)  

    print count  

   

folderFilesLines('D:/study/practice/algorithm')     

#countFolderCppFilesLines('D:/study/practice/algorithm')  

代码还没整理,应该可以简化,先用这个 吧

点击复制链接 与好友分享!回本站首页
上一篇:Python建最小堆
下一篇:Code is to be happy
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站