软件著作权统计代码行数
最近在申请软件著作权,其中有要求要统计行数,发现网上的版本都不尽人意,往往只能统计一层,所以我就改进了一下😁,改成了递归调用版本,这样就不需要再手动遍历文件夹了
#coding:utf-8 import os class StatLines(object): total = 0 def stat_lines(self,path): for root, dirs, files in os.walk(path, topdown=False): for file in files: if file.endswith('.py'): file = os.path.join(root, file) lines = open(file, encoding='utf-8').readlines() count = 0 for line in lines: if line == '\n': continue elif line.startswith('#'): continue else: count += 1 self.total += count print('%s has %d lines' %(file,count)) for dir in dirs: path = os.path.join(root, dir) self.stat_lines(path) print('total lines is: %d' %self.total) if __name__ == '__main__': sl = StatLines() sl.stat_lines('想要统计的文件夹路径')