以前写了一个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统计代码行数的更多相关文章

  1. 007-使用python统计代码行数,空行以及注释

    # 自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但是要分别列出来 1.打开文件方法 1.1 以读文件的模式打开一个文件对象,使用Python内置的open()函数,传入文件名和标示符 f ...

  2. 利用python统计代码行

    参加光荣之路测试开发班已三月有余,吴总上课也总问“ 咱们的课上了这么多次了大家实践了多少行代码了?”.这里是一个一脸懵逼的表情.该怎么统计呢?一个个文件数当然不可取,能用代码解决的事咱们坚决不动手.最 ...

  3. Eclipse统计代码行数

    开发过程中,经常需要统计代码行数,这时可以通过Eclipse的Search功能来实现. 步骤: 1.在Package Explorer中选中需要统计的包: 2.单击菜单Search-->File ...

  4. 在Flash Builder或者Eclipse统计代码行数的方法

    在Flash  Builder或者Eclipse统计代码行数的方法如下图菜单栏--搜索--搜索文件

  5. 【Linux】常用命令-统计代码行数

    公司人员流动大,经常有新的维护任务,交接时喜欢看看新来的模块的代码量,那么问题来了, 如何统计代码行数? 1,最先想到的肯定是 wc. wc -l *.h 将查看[当前目录]下头文件的代码行数,输出结 ...

  6. c#统计代码行数

    小编,已经快学了两年编程了.昨天突发奇想,想统计下这些年到底写过多少行代码,于是做了一个这个小程序来统计代码行数.老规矩,先上图. 比较惭愧,写了两年只有2万多行.那我们还是进入下一项吧. 界面搭建我 ...

  7. 【未解决】对于使用Windows的IDEA进行编译的文件,但无法在Linux系统中统计代码行数的疑问

    在我学习使用Windows的IDEA的过程中,将代码文件转移到Linux虚拟机当中,但无法在Linux系统中统计代码行数. 注意:拷贝进虚拟机的文件均能编译运行. 具体过程如下: root@yogil ...

  8. Visual Studio 统计代码行数

    介绍一种简单的统计代码行数的小技巧, 使用正则表达式,用VS强大的查找功能 b[^:b#/]+.$ 最后结果:

  9. 用django统计代码行数+注释行数

    实现统计代码行数: 1.首先在url.py中配置 from django.conf.urls import url from django.contrib import admin from app0 ...

随机推荐

  1. iOS学习笔记(六)——ViewController

    ViewController是iOS应用程序中重要的部分,是应用程序数据和视图之间的重要桥梁,ViewController管理应用中的众多视图.iOS的SDK中提供很多原生ViewController ...

  2. hdu 5452(树链刨分)

    看到题目,想了挺长时间,发现不会,然后看着样子像是树上成段操作,所以查了下树链刨分,结果真的就是这个东西... Minimum Cut Time Limit: 3000/2000 MS (Java/O ...

  3. 【转】NPOI自定义单元格背景颜色

    经常在NPOI群里聊天时发现有人在问NPOI设置单元格背景颜色的问题,而Tony Qu大神的博客里没有相关教程,刚好最近在做项目时研究了一下这一块,在这里总结一下. 在NPOI中默认的颜色类是HSSF ...

  4. springboot之修改内置tomcat配置项

    1.spring boot默认端口号是8080,如果要修改端口的话,只需要修改application.properties文件,在其中加入 例如: server.port=8081 2.在正常的项目中 ...

  5. 【Python之路】第十五篇--Web框架

    Web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. #!/usr/bin/env python #coding:utf- ...

  6. entropy 压缩信息的熵更加高 实际上英文文本的熵大概只有4.7比特

    https://en.wikipedia.org/wiki/Entropy_(information_theory) https://zh.wikipedia.org/wiki/熵(信息论) 熵的概念 ...

  7. JavaScript方法splice()和slice()

    1 splice() 1.1 说明 splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目.该方法会改变原始数组.Link 1.2 语法 arrayObject.splice(inde ...

  8. python基础里的那些为什么?

    一.执行python脚本的两种方式? 直接在解释器里编写并在解释器里执行 文件编写,并在终端通过 python 路径  这种方式执行 好,我们就以输出hello world这个例子来比较两种方式的不同 ...

  9. Insert Buffering

    14.5.13.4 Insert Buffering Database applications often insert new rows in the ascending order of the ...

  10. nodejs get请求

    const http = require('http'); http.get('http://192.168.1.6:8080/getDemo?msg=12', (res) => { const ...