日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python实现统计你一共写了多少行代码

發布時間:2024/7/23 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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实现统计你一共写了多少行代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。