每天一个python小程序 007有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。

#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)
    
    
    
    
    
    
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值