当前位置:编程学习 > 网站相关 >>

统计文件夹内.c .cpp .py类型文件的代码行数

[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')  
代码还没整理,应该可以简化,先用这个 吧
补充:Web开发 , Python ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,