#coding=utf-8
"""
有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
"""
import sys,os,re
def each(path):
all=[]
for root,dirs,files in os.walk(path):
#print "root,dirs,files",root,dirs,files
for name in files:
all.append(root+"/"+name)
return all
def deal(input):
if os.path.splitext(input)[1] in [".py",".pyw"]:
total,comment,empty=0,0,0
f=open(input,"r")
in_comment=False
for line in f:
total+=1
if re.findall("\"\"\"$",line):
if in_comment:
in_comment = False
else:
in_comment = True
if not re.findall("\S",line):
empty+=1
if line[0]=="#" or in_comment:
comment+=1
return total,comment,empty
else:
return 0,0,0
if __name__ == '__main__':
if len(sys.argv)<=1:
path = os.path.split(os.path.realpath(__file__))[0]+"/"
print "The Script will calculate the LOC of the file in "+path
else:
print "calculating the file in "+sys.argv[1]
if os.path.isdir(sys.argv[1]):
path = sys.argv[1]
else:
print "Path Error! use this script as "+os.path.split(os.path.realpath(__file__))[1]+" [path]"
#print "-------------------------"
#print each(path)
a,c,e=0,0,0
for i in each(path):
ta,tc,te=deal(i)
a+=ta
c+=tc
e+=te
print "Total lines: %s. Empty lines: %s. Comment Lines: %s." % (a, e, c)
每天一个python小程序 007有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
最新推荐文章于 2021-02-25 19:14:40 发布