python实现统计你一共写了多少行代码
生活随笔
收集整理的這篇文章主要介紹了
python实现统计你一共写了多少行代码
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
程序員要保證一定的代碼量就必須勤奮的敲代碼,但怎么知道自己一共寫了多少代碼呢,筆者用python寫了個(gè)簡(jiǎn)單的腳本,遍歷所有的.java,.cpp,.c文件的行數(shù),但是正如大家所知,java生成了許多代碼,所以有許多水分,準(zhǔn)確性并不太高,只具有一定的參考價(jià)值。
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('行數(shù)為:',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('行數(shù)為:',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('行數(shù)為:',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('行數(shù)為:',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",如果沒有這個(gè)的話,默認(rèn)使用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('行數(shù)為:',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('行數(shù)為:',count)i=iforjava+iforc+iforcpp print('總行數(shù)為:',i) lineall=str(i)g.msgbox("嗨,你一共寫了"+lineall+"行代碼,要繼續(xù)加油哦^_^") g.msgbox("其中\(zhòng)nC語言"+str(iforc)+"行\(zhòng)nC++"+str(iforcpp)+"行\(zhòng)njava"+str(iforjava)+"行")os.system('pause')在打開文件的時(shí)候,老是因?yàn)镚BK編碼與UTF-8編碼出錯(cuò),因?yàn)椴恢牢募木幊谈袷?#xff0c;所以會(huì)以錯(cuò)誤的編碼方式打開,所說可以通過chardet包解決,但似乎還沒有引入到python3中,所以只能手動(dòng)改。。。。
運(yùn)行截圖如下:
總結(jié)
以上是生活随笔為你收集整理的python实现统计你一共写了多少行代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: postgres与osm初步使用
- 下一篇: C站最全Python库总结丨标准库+高级