Python小工具——统计文件夹下源代码的行数
来源:互联网 发布:查找算法 编辑:程序博客网 时间:2024/05/19 03:29
趁着热情着,又赶了份统计源代码的小工具
程序是统计java文件的,以后有机会更新成统计各个源代码版本的程序
统计目录下每个文件的总行数,正常行数,注释行数,空白行数
和整个目录下总的 这些行数。
程序运行后,自动在指定目录生成一个result.txt文件
同样的利用cmd, python generate.py 文件目录
代码
# -*- coding: cp936 -*-#author SUMimport os,sysdef 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_listdef 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_filenameif __name__=='__main__': root_dir=sys.argv[1] count_files(root_dir,['.java'])
0 0
- Python小工具——统计文件夹下源代码的行数
- 统计某文件夹下源代码行数 Python脚本
- python 统计指定文件夹下的某类文本文件行数
- Python小工具之文件夹下搜索包含关键字的文件
- 小工具:批量转换文件夹下的特定类…
- Mac下统计任意文件夹中代码行数的工具——cloc
- Mac下统计任意文件夹中代码行数的工具——cloc
- 统计 apk 方法数和代码行数小工具
- 一个Python的小工具
- python小工具:批量替换某个目录下的文件名
- VC下的小工具
- ethtool小工具的源代码网址
- 实现统计java程序代码的小工具
- UNITY 用于打点统计的小工具
- [小工具]代码统计小工具编写
- HBASE自带小工具,统计表的行数
- 统计一个目录下源代码行数
- 统计文件夹下java代码行数
- 为什么要使用Hibernate
- iOS开发常用的第三方类库
- 画8
- 项目进度
- JAVA hdu 1316 How Many Fibs?(高精度)
- Python小工具——统计文件夹下源代码的行数
- 外观模式
- Oracle 安装之ODAC:ODTwithODAC1110621
- IOS二维码的实现
- c/c++ string和double类型互转
- 窗口消息
- Jquery Mobile学习教程之四 页面过渡
- 算法之旅 直奔DP
- 实现了Runnable接口的实例,就一定会启动一个新的线程么?