python统计代码行数
以前写了一个java的统计代码行数的小程序,最近在看python,于是就参考前辈的代码,写了一个统计文件夹下面各种程序的代码的小程序,这里贴出来供大家参考
参考链接:
https://gist.github.com/linmingren/58d14b5a5fa24e6410af
http://www.cnblogs.com/MikeZhang/archive/2012/08/23/linesCountPython20120823.html
1.运行效果:

2.主要的包,函数:
os.path
os.walk(path)
os.path.split(path)[1]
3..代码:
# -*- coding: utf-8 -*- """
this is the moduler to count code lines in the given directory.
It ignore blank line but counts the note line.such as #...,//...
__author__ = 'R'
time: 2017.4.21
""" import sys,os filetype = ['.c', '.cpp', '.java', '.py', '.h']
linescount = 0
filescount = 0
c_count = 0
cpp_count = 0
java_count = 0
py_count = 0
h_count = 0 defaultPath = 'D://programmer/python' def countLines(filepath):
global c_count,cpp_count,java_count,py_count,h_count
lines = 0
#这里打开文件经常有无法编码或者无法解码的问题;
with open(filepath, 'rb') as f:
temp = os.path.split(filepath)[1]
for line in f:
if line.strip() != '':
if temp.endswith(filetype[0]):
c_count += 1
lines += 1
elif temp.endswith(filetype[1]):
cpp_count += 1
lines += 1
elif temp.endswith(filetype[2]):
java_count += 1
lines += 1
elif temp.endswith(filetype[3]):
py_count += 1
lines += 1
elif temp.endswith(filetype[4]):
h_count += 1
lines += 1
#这样会导致将所有文件的行数都统计进去了,比如.txt,二进制;
#lines += 1
#print(line)
return lines def countFilesLines(path):
lines = 0
global linescount,filescount
for rootpath, subdirs, filenames in os.walk(path):
for f in filenames:
lines += countLines(os.path.join(rootpath, f))
filescount += 1
linescount = lines
return
if __name__ == '__main__':
print ('counting...')
if len(sys.argv) >= 2:
countFilesLines(sys.argv[1])
else:
countFilesLines(defaultPath)
print ("total files: " , str(filescount))
print ('total code lines: ' + str(linescount))
print ('total c conde lines: ' + str(c_count))
print ('total cpp code lines: ' + str(cpp_count))
print ('total java code lines: ' + str(java_count))
print ('total python code lines: ' + str(py_count))
print ('total .h code lines: ' + str(h_count))
python统计代码行数的更多相关文章
- 007-使用python统计代码行数,空行以及注释
# 自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但是要分别列出来 1.打开文件方法 1.1 以读文件的模式打开一个文件对象,使用Python内置的open()函数,传入文件名和标示符 f ...
- 利用python统计代码行
参加光荣之路测试开发班已三月有余,吴总上课也总问“ 咱们的课上了这么多次了大家实践了多少行代码了?”.这里是一个一脸懵逼的表情.该怎么统计呢?一个个文件数当然不可取,能用代码解决的事咱们坚决不动手.最 ...
- Eclipse统计代码行数
开发过程中,经常需要统计代码行数,这时可以通过Eclipse的Search功能来实现. 步骤: 1.在Package Explorer中选中需要统计的包: 2.单击菜单Search-->File ...
- 在Flash Builder或者Eclipse统计代码行数的方法
在Flash Builder或者Eclipse统计代码行数的方法如下图菜单栏--搜索--搜索文件
- 【Linux】常用命令-统计代码行数
公司人员流动大,经常有新的维护任务,交接时喜欢看看新来的模块的代码量,那么问题来了, 如何统计代码行数? 1,最先想到的肯定是 wc. wc -l *.h 将查看[当前目录]下头文件的代码行数,输出结 ...
- c#统计代码行数
小编,已经快学了两年编程了.昨天突发奇想,想统计下这些年到底写过多少行代码,于是做了一个这个小程序来统计代码行数.老规矩,先上图. 比较惭愧,写了两年只有2万多行.那我们还是进入下一项吧. 界面搭建我 ...
- 【未解决】对于使用Windows的IDEA进行编译的文件,但无法在Linux系统中统计代码行数的疑问
在我学习使用Windows的IDEA的过程中,将代码文件转移到Linux虚拟机当中,但无法在Linux系统中统计代码行数. 注意:拷贝进虚拟机的文件均能编译运行. 具体过程如下: root@yogil ...
- Visual Studio 统计代码行数
介绍一种简单的统计代码行数的小技巧, 使用正则表达式,用VS强大的查找功能 b[^:b#/]+.$ 最后结果:
- 用django统计代码行数+注释行数
实现统计代码行数: 1.首先在url.py中配置 from django.conf.urls import url from django.contrib import admin from app0 ...
随机推荐
- python3----字符串中的字符倒转
方法一,使用[::-1]: s = 'python' print(s[::-1]) 方法二,使用reverse()方法: n = list(s) n.reverse() print(''.join(n ...
- node.js的安装与第一个hello world、node.js的初始化
1.下载node.js文件 2.windows下点击安装 重复下一步即可 3.编辑工具 EditPlus编辑器 4.新建保存目录的文件夹,并新建一个文本文档 5.打开EditPlus编辑器 打开 ...
- iOS学习笔记(九)—— xml数据解析
在iPhone开发中,XML的解析有很多选择,iOS SDK提供了NSXMLParser和libxml2两个类库,另外还有很多第三方类库可选,例如TBXML.TouchXML.KissXML.Tiny ...
- K - Children of the Candy Corn(待续)
K - Children of the Candy Corn Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d ...
- mix-in class selectors
语言特性 | Less 中文网 http://lesscss.cn/features/#mixins-feature Mixins "mix-in" properties from ...
- ssm框架整合-过程总结(第二次周总结)
距离上次写博客已经有4.5天的时间了. 这次写博客目的是总结一下项目开始到现在,过程中遇到的问题.和学到的知识.经验. 初略总结下自己从中学到的: Spring :在学习中被反复强调的Ioc(反转控制 ...
- JSP页面获取下来框select选中项的值和文本的方法
<select id="username" name=""> <option value="1">jyy< ...
- 2014-08-28——移动端,触摸事件 touchstart、touchmove、touchend、touchcancel
1.Touch事件简介在移动终端上的web页面触屏时会产生ontouchstart.ontouchmove.ontouchend.ontouchcancel 事件,分别对应了触屏开始.拖拽及完成触屏事 ...
- Linux中的流程控制语句
if语句 if [ 条件判断式 ] then 程序elif [ 条件判断式 ] then 程序else 程序fi 注意: a.使用fi结尾 b.条件判断式和中括号之间需要有空格 [root@local ...
- ASP.NET MVC string赋值Html格式在显示View问题总结
ViewBag.Content = "<p>你好</p>"; string 类型的赋值一个 "<h1>你好</h1>&qu ...