程序员要保证一定的代码量就必须勤奋的敲代码,但怎么知道自己一共写了多少代码呢,笔者用python写了个简单的脚本,遍历所有的.java,.cpp,.c文件的行数,但是正如大家所知,java生成了许多代码,所以有许多水分,准确性并不太高,只具有一定的参考价值。

import os
import easygui as g
import sys
import chardet
path = 'C:/'
path='D:/Data/CProject/dataStruct/PAT/PAT'
path='F:/Workspaces'
path2='D:/Data/CProject'
path3='D:/WorkSpace/SoftWare/cocos2d-x-2.2.3/projects'
path4='F:/MyWrokspace'
iforjava=0;
iforcpp=0;
iforc=0; for root, dirs, files in os.walk(path):
#files=''.join(files) #print(type(files))
for str1 in files:
if 'java'==str1.split('.').pop():
print("Root = ", root, "dirs = ", dirs, "files = ", str1)
count= len(open(root+'\\'+str1,'rU',encoding= 'gbk').readlines())
iforjava=iforjava+int(count)
print('行数为:',count) for root, dirs, files in os.walk(path2):
#files=''.join(files) #print(type(files))
for str1 in files:
if 'c'==str1.split('.').pop():
print("Root = ", root, "dirs = ", dirs, "files = ", str1)
count= len(open(root+'\\'+str1,'rU',encoding= 'gbk').readlines())
iforc=iforc+int(count)
print('行数为:',count)
if 'cpp'==str1.split('.').pop():
print("Root = ", root, "dirs = ", dirs, "files = ", str1)
count= len(open(root+'\\'+str1,'rU',encoding= 'gbk').readlines())
iforcpp=iforcpp+int(count)
print('行数为:',count) for root, dirs, files in os.walk(path3):
#files=''.join(files) #print(type(files)) for str1 in files:
if 'c'==str1.split('.').pop():
print("Root = ", root, "dirs = ", dirs, "files = ", str1)
count= len(open(root+'\\'+str1,'rU',encoding= 'gbk').readlines())
iforc=iforc+int(count)
print('行数为:',count)
if 'cpp'==str1.split('.').pop() and len(str1.split('.'))==2 and root.split('\\').pop()!='proj.wp8' and root.split('\\').pop()!='proj.winrt':
print("Root = ", root, "dirs = ", dirs, "files = ", str1)
count= len(open(root+'\\'+str1,'rU',encoding= 'gbk').readlines())
'''file = open(root+'\\'+str1, "rb")#要有"rb",如果没有这个的话,默认使用gbk读文件。
buf = file.read()
result = chardet.detect(buf)
file = open(root+'\\'+str1,"r",encoding=result["encoding"])
count=len(file.readlines())'''
iforcpp=iforcpp+int(count)
print('行数为:',count) for root, dirs, files in os.walk(path4):
#files=''.join(files) #print(type(files))
for str1 in files:
if 'java'==str1.split('.').pop()and root.split('\\').pop()!='style':
print("Root = ", root, "dirs = ", dirs, "files = ", str1)
count= len(open(root+'\\'+str1,'rU',encoding= ('gbk' or 'utf-8')).readlines())
iforjava=iforjava+int(count)
print('行数为:',count) i=iforjava+iforc+iforcpp
print('总行数为:',i)
lineall=str(i) g.msgbox("嗨,你一共写了"+lineall+"行代码,要继续加油哦^_^")
g.msgbox("其中\nC语言"+str(iforc)+"行\nC++"+str(iforcpp)+"行\njava"+str(iforjava)+"行") os.system('pause')

在打开文件的时候,老是因为GBK编码与UTF-8编码出错,因为不知道文件的编程格式,所以会以错误的编码方式打开,所说可以通过chardet包解决,但似乎还没有引入到python3中,所以只能手动改。。。。

运行截图如下:





python实现统计你一共写了多少行代码的更多相关文章

  1. 开源一个Mac漂亮的小工具 PPRows for Mac, 在Mac上优雅的计算你写了多少行代码

    开源一个Mac漂亮的小工具 PPRows for Mac, 在Mac上优雅的计算你写了多少行代码. 开源地址: https://github.com/jkpang/PPRows

  2. 写一个程序,统计自己C语言共写了多少行代码。ver2.00

    概要 完成一个程序,作用是统计一个文件夹下面所有文件的代码行数.输入是一个文件夹的绝对路径,输出是代码行数.所以此程序的新特点有两个: 统计某一文件夹下的所有文件: 可以任意指定本机硬盘上任何位置的某 ...

  3. python爬煎蛋妹子图--20多行代码搞定煎蛋妹子图库

    如果说一个人够无聊的话... 就会做一些十分美(wei)丽(suo)的事情啦哈哈哈... 好的,话不多说,进入正题. 正如标题所示,我们今天的目标很简单: 代码要少,妹子要好. 步骤如下: 1. 首先 ...

  4. 写一个程序,统计自己C语言共写了多少行代码,Github基本操作

    前言 在上一篇博客中,本人提到了自己的文件操作可以说是几乎没用过.现在想想,这也算是只在OJ上做题的一个弊端吧.虽然通过OJ做题是一个学习代码好手段,但其他方面也要多多涉猎才好,而不是说OJ用不到文件 ...

  5. Python进阶系列之怎么写出pythonic的代码

    使用 in/not in 检查key是否存在于字典中 判断某个key是否存在于字典中时,一般的初学者想到的方法是,先以列表的形式把字典所有的key返回,在判断该key是否存在于key列表中 d = { ...

  6. 帮同事写了几行代码,在 安装/卸载 程序里 注册/卸载 OCX控件

    写了个小控制台程序,这个程序用来注册 / 卸载OCX控件,用在Inno Setup做的安装卸载程序里. #include "stdafx.h" #include <windo ...

  7. Python人脸识别最佳教材典范,40行代码搭建人脸识别系统!

    Face Id是一款高端的人脸解锁软件,官方称:"在一百万张脸中识别出你的脸."百度.谷歌.腾讯等各大企业都花费数亿来鞭策人工智能的崛起,而实际的人脸识别技术是否有那么神奇? 绿帽 ...

  8. Python+moviepy使用manual_tracking和headblur函数10行代码实现视频人脸追踪打马赛克

    ☞ ░ 前往老猿Python博文目录 ░ 一.背景知识 1.1.headblur简介 追踪人脸打马赛克需要使用headblur函数. 调用语法: headblur(clip,fx,fy,r_zone, ...

  9. 图片全部加载完成之后再显示页面ui,公司项目里用上,自己写的几行代码

    说明: -----onload事件   这里我并没有考虑ie的兼容性 因为项目是移动端的: -----求大神指正~ -----自己测试正常 页面没加载完之前会有一个提示 /************** ...

随机推荐

  1. MyEclipse------随机流(能读也能写数据)

    RandomAccessFile流指向文件时,不刷新文件. 其中seek(long a)用来定位RandomAccessFile流的读写位置,其中参数a确定读写位置距离文件开头的字节个数. other ...

  2. 伪分布模式下执行wordcount实例时报错解决办法

    问题1.不能分配内存,错误提示如下: FAILEDjava.lang.RuntimeException: Error while running command to get file permiss ...

  3. http https 区别

    HTTPS和HTTP的区别 一.https协议需要到ca申请证书,一般免费证书很少,需要交费.  二.http是超文本传输协议,信息是明文传输,https 则是具有安全性的ssl加密传输协议.  三. ...

  4. java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration解决方法

    Autowiring of fields failed; nested exception is...........Error creating bean with name 'siteOperat ...

  5. c#之Redis实践list,hashtable

    写在前面 最近公司搞了一个活动,用到了redis的队列,就研究了下redis的相关内容.也顺手做了个demo. C#之使用Redis 可以通过Nuget安装Reidis的相关程序集.安装之后发现会引入 ...

  6. linux下ping的C语言实现(转)

    #include <stdio.h> #include <signal.h> #include <arpa/inet.h> #include <sys/typ ...

  7. 如何在word里面插入目录

    点击“引用”->插入目录

  8. "微信全球商业创新大赛-创意中国2015"国际MBA商业挑战赛开启

    微信商业化进程一直在摸索中前进,从未间断.近日由中欧国际工商学院与腾讯(Tencent)共同发起举办“微信全球商业创新大赛 - 创意中国2015”(We Win with WeChat - INNOV ...

  9. A+B Again(在某个数中找大于m的最小约数)

    A+B Again Accepted : 15   Submit : 243 Time Limit : 1000 MS   Memory Limit : 65536 KB  题目描述 上次趣味赛小明的 ...

  10. 《Hadoop基础教程》之初识Hadoop

    Hadoop一直是我想学习的技术,正巧最近项目组要做电子商城,我就开始研究Hadoop,虽然最后鉴定Hadoop不适用我们的项目,但是我会继续研究下去,技多不压身. <Hadoop基础教程> ...