统计文件夹内指定类型文件的代码行数(二)
#_*_coding:utf_8_import osimport globdef 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 countdef folderCodeLines(folderpath): count = 0 filetype = ['*.py', '*.c', '*.cpp'] #指定需要统计的文件类型的列表 for type in filetype: print type filepath = str(folderpath) + '/' + str(type) for file in glob.glob(filepath): count += countFileLines(file) print count return countfolderCodeLines('D:/study/practice/algorithm')