统计某文件夹下源代码行数 Python脚本
2010-01-07 19:25
429 查看
写了一个统计某文件夹下各种源代码行数的脚本
仅支持c/c++ java python
有需要可以自己更改程序,我把这个脚本的拓展性做的很高
很容易使之支持其他文本型语言代码
或者给我Email我帮你做 onezeros@yahoo.cn
对于MFC自动生成的代码应该排除在外
可以用一种不太严密的方法:先统计各种mfc生成工程代码数量
然后在分析文件夹时根据mfc文件的一些特征判断目标文件夹是否为mfc工程
如果是,就减去生成的代码量
下图是我的一个文件夹的统计结果
仅支持c/c++ java python
有需要可以自己更改程序,我把这个脚本的拓展性做的很高
很容易使之支持其他文本型语言代码
或者给我Email我帮你做 onezeros@yahoo.cn
对于MFC自动生成的代码应该排除在外
可以用一种不太严密的方法:先统计各种mfc生成工程代码数量
然后在分析文件夹时根据mfc文件的一些特征判断目标文件夹是否为mfc工程
如果是,就减去生成的代码量
#!/usr/bin/python #codelines.py short for files in directory import sys import os import os.path class Element: _fileamount=0#number of files _codelines=0 #line number of files _commandlines=0;#standalone command lines extension={'c':0,'cpp':0,'C':0,'cc':0,'cxx':0, 'h':0,'hpp':0,'hxx':0,'java':1,'py':2} language=['c/c++','java','py'] statistics=[Element() for x in range(0,len(language))] #count lines in a file def fileparser(file,ext): global extension,statistics try: f=open(file,'r') except: print("failed to open "+file) print("parsing file "+file) #print("extension "+str(extension[ext])) statistics[extension[ext]]._fileamount+=1 try: context=f.readlines() i=-1; while i<len(context): i+=1 line=context[i].strip() if line=='': continue else: if extension[ext]==0 or ext=='java':#c/cpp#java if line[0:2]=='//': statistics[extension[ext]]._commandlines+=1 elif line[0:2]=='/*': while line.find('*/')==-1: i+=1; line=context[i].strip() statistics[extension[ext]]._commandlines+=1 statistics[extension[ext]]._commandlines+=1 else: statistics[extension[ext]]._codelines+=1 elif ext=='py':#python if line[0]=='#': statistics[extension[ext]]._commandlines+=1 else: statistics[extension[ext]]._codelines+=1 except: pass f.close() return #find source code file by analyzing extension def extparser(file): global extension ext=os.path.splitext(file)[1][1:] #print("extension "+ext) if ext in extension: #print("in extparing "+file) fileparser(file,ext) else: pass return #parse a directory to find files def dirparser(directory): print("parsing directory "+directory) try: dirlist=os.listdir(directory) for i in dirlist: fullpath=directory+'//'+i if(os.path.isdir(fullpath)==True): dirparser(fullpath) else: extparser(fullpath) except: pass return if __name__ == "__main__": if(os.path.isdir(sys.argv[1])==False): print('%s is not a correct path',sys.argv[1]); sys.exit(0) dirparser(sys.argv[1]) length=[len('language: '),len('file amount: '), len('code amount: '),len('command amount')] print('language: file amount: code amount: command amount') for i,lang in enumerate(language): print(repr(lang).ljust(length[0]), repr(statistics[i]._fileamount).ljust(length[1]), repr(statistics[i]._codelines).ljust(length[1]), repr(statistics[i]._commandlines).ljust(length[1])) print('Done')
下图是我的一个文件夹的统计结果
相关文章推荐
- Python小工具——统计文件夹下源代码的行数
- python2.7.3 统计某个文件夹下所有文件或者某个文件的行数
- 统计文件夹下代码行,python脚本,linux可用
- Python脚本实现代码行数统计代码分享
- Linux下统计某个目录下所有源代码行数的Shell脚本;
- 统计python脚本中 shell命令输出文本的行数
- python 脚本(获取指定文件夹、指定文件格式、的代码行数、注释行数)
- Python统计脚本行数(fileinput)
- Linux下统计当前目录下源代码行数的Shell脚本
- Python脚本:TXT文档行数统计
- Linux下统计某个目录下所有源代码行数的Shell脚本
- python脚本: 双向统计文件字符、单词数、行数
- 利用Python统计源代码行数以及对源代码排版
- Linux下统计某个目录下所有源代码行数的Shell脚本
- Python:统计特定文件夹中,特定文件代码行数的小程序
- Python脚本实现代码行数统计代码分享
- python 脚本(获取指定文件夹、指定文件格式、的代码行数、注释行数)
- Python 统计代码的行数,Python脚本 统计代码
- python 统计指定文件夹下的某类文本文件行数
- 代码行数统计的Java和Python实现