python实现统计你一共写了多少行代码
生活随笔
收集整理的這篇文章主要介紹了
python实现统计你一共写了多少行代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
程序員要保證一定的代碼量就必須勤奮的敲代碼,但怎么知道自己一共寫了多少代碼呢,筆者用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实现统计你一共写了多少行代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: postgres与osm初步使用
- 下一篇: FragmentPagerAdapter