Python小工具——统计文件夹下源代码的行数


趁着热情着,又赶了份统计源代码的小工具


程序是统计java文件的,以后有机会更新成统计各个源代码版本的程序

统计目录下每个文件的总行数,正常行数,注释行数,空白行数

和整个目录下总的 这些行数。


程序运行后,自动在指定目录生成一个result.txt文件


同样的利用cmd, python generate.py 文件目录



代码


# -*- coding: cp936 -*-
#author SUM
import os,sys
def analyze(target_file,file_list):


        commentAll=0
        whiteAll=0
        normalAll=0
        for files in file_list:
                with open(files) as file:
                        lines=file.readlines()
                file.close()
                target_file.write(file.name+"\n")
                commentLines=0
                whiteLines=0
                normal=0
                comment=False

                for i in lines:
                        i=i.strip()
                        if(i==''):
                                whiteLines=whiteLines+1
                        elif(i.startswith('/*') and False==i.endswith('*/')):
                                commentLines=commentLines+1
                                comment=True
                        elif(i.startswith('/*') and True==i.endswith('*/')):
                                commentLines=commentLines+1
                        elif(i.startswith('//')):
                                commentLines=commentLines+1
                        elif(True==comment):
                                commentLines=commentLines+1
                                #print i
                                if(i.endswith('*/')):
                                       # print i
                                        comment=False
                        else:
                                normal=normal+1
                target_file.write('%s%d'%('normal is ',normal)+'\n')
                target_file.write('%s%d'%('  whiteLines is ',whiteLines)+'\n')
                target_file.write('%s%d'%('  commentLines is ',commentLines)+'\n')
                commentAll=commentAll+commentLines
                normalAll=normalAll+normal
                whiteAll=whiteAll+whiteLines
        target_file.write('所有文件总行数:')
        target_file.write('%d'%(commentAll+normalAll+whiteAll)+'\n')
        target_file.write('正常代码总行数:')
        target_file.write('%d'%(normalAll)+'\n')
        target_file.write('注释总行数:')
        target_file.write('%d'%(commentAll)+'\n')
        target_file.write('空白总行数:')
        target_file.write('%d'%(whiteAll)+'\n')


def get_process_files(root_dir,file_filter):
    """process all files in directory that match file_filter"""
    cur_dir=os.path.abspath(root_dir)
    file_list=os.listdir(cur_dir)
    #print file_list
    process_list=[]
    #process process_list return
    for file in file_list:
        fullfile=cur_dir+"\\"+file
        #print fullfile,
        if os.path.isfile(fullfile):
            #print 'is file',
            found_flag=False
            for filter in file_filter:
                if file.rfind(filter)!=-1:
                    #print 'match',
                    process_list.append(fullfile)
                    found_flag=True
            #if found_flag==False:
                #print# "pass this file"
        elif os.path.isdir(fullfile):
            #print
            dir_extra_list=get_process_files(fullfile,file_filter)
            if len(dir_extra_list)!=0:
                for x in dir_extra_list:
                    process_list.append(x)
            
            #print dir_extra_list
            #if len(dir_extra_list)!=0:
                #process_list.append(dir_extra_list)
        else:
            print 'not defined'
    return process_list

def count_files(root_dir,file_filter):
    process_list=get_process_files(root_dir,file_filter)

    target_filename=root_dir+"\\"+"result.txt"
    with open(target_filename,'w') as target_file:
        target_file.write("//"+root_dir+"//\n")
        
        analyze(target_file,process_list)#打开文件分析注释
        target_file.close()
    print 'Result is generated in',target_filename


if __name__=='__main__':
        root_dir=sys.argv[1]
        count_files(root_dir,['.java'])


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值