当前位置: 移动技术网 > IT编程>脚本编程>Python > Python小工具——统计文件夹下源代码的行数

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

2019年04月19日  | 移动技术网IT编程  | 我要评论
趁着热情着,又赶了份统计源代码的小工具 程序是统计java文件的,以后有机会更新成统计各个源代码版本的程序 统计目录下每个文件的总行数,正常行数,注释行数,空白行数 和整个


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


程序是统计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'])


如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网