还说啥呀,直接上代码趴~
# coding=utf-8
"""
统计源码行数及字数脚本
by Joy
"""
import os
import glob
# 给定源码文件夹
dirPath = r"D:\SVN\Python\ftp"
# 统计文件类型的元组,可以依据格式自行添加类型
suffixTuple = ("py","java","json","c")
def counter():
global dirPath
# 路径格式纠错
if dirPath.endswith("\\"):
dirPath = dirPath.strip("\\")
elif dirPath.endswith("/"):
dirPath = dirPath.strip("/")
if not os.path.isdir(dirPath):
print "目标文件夹不存在...请检查"
# 行数
lineCounter = 0
# 字符数
charCounter = 0
for root, dirs, files in os.walk(dirPath):
index = 0
while index < len(suffixTuple):
# 获取文件夹下所有匹配文件的绝对路径
for filePath in glob.glob(root+r"\*.{}".format(suffixTuple[index])):
if filePath and os.path.isfile(filePath):
with open(filePath,"r") as fr:
for line in fr.readlines():
# 简单对python及java中的注释行及空白行做了个过滤,有待改进
if not line.startswith("#") or line.startswith("//") or line.strip()=="":
lineCounter += 1
charCounter += len(line)
else:
print "{}文件不存在...".format(filePath)
index +=1
print "源码行数为:{} ,字符数为:{}".format(lineCounter,charCounter)
if __name__ == "__main__":
counter()