晚上想统计一下我的java代码行数,于是用python实现了一下,真的太简单,30行不到就搞定了 # -*- encoding: utf-8 -*- import os class SearchFile(object): def __init__(self,stype): self.jsflist = [] self.stype = stype self.totalNum = 0 self.totalline = 0 def getTotalCodeLine(self): for filename in self.jsflist: f = open(filename) fileLines = f.readlines() fls = len(fileLines) self.totalline += fls f.close() def search(self,sdir): self.rootdir = sdir os.path.walk(sdir, self.searchfile, None) self.getTotalCodeLine() def searchfile(self,arg,dirname,names): for jfname in names: fulljavafilename = os.path.join(dirname,jfname) if(os.path.isfile(fulljavafilename)): if os.path.splitext(jfname)[1] == self.stype: self.totalNum +=1 self.jsflist.append(fulljavafilename) if __name__ == "__main__": searchfile = SearchFile('.java') searchfile.search('/wch/dev/workspace/java/090326/ezweb2.0-core/src/com/ergal/ezweb/core')