Python實(shí)現(xiàn)代碼統(tǒng)計(jì)工具
本文實(shí)例為大家分享了Python實(shí)現(xiàn)代碼統(tǒng)計(jì)工具的具體代碼,供大家參考,具體內(nèi)容如下
思路:首先獲取所有文件,然后統(tǒng)計(jì)每個(gè)文件中代碼的行數(shù),最后將行數(shù)相加.
實(shí)現(xiàn)的功能:
統(tǒng)計(jì)每個(gè)文件的行數(shù);
統(tǒng)計(jì)總行數(shù);
支持指定統(tǒng)計(jì)文件類型,排除不想統(tǒng)計(jì)的文件類型;
排除空行;
排除注釋行
import os
import sys
import os.path
#for i in sys.argv:
# print (i)
# 判斷單個(gè)文件的代碼行數(shù)
def count_file_lines(file_path):
line_count = 0
flag=True
try:
fp = open(file_path,"r",encoding="utf-8")
encoding_type="utf-8"
fp.close()
except:
encoding_type="gbk"
with open(file_path,"r",encoding=encoding_type) as fp:
for line in fp:
#print (line_count)
if line.strip()=="":
continue
else:
if line.strip().endswith("'''") and flag == False:
flag=True
continue
if line.strip().endswith('"""') and flag == False:
flag=True
continue
if flag == False:
continue
if line.strip().startswith("#encoding") or line.strip().startswith("#-*-"):
line_count += 1
#elif line.strip().startswith('"""') and line.strip().endswith('"""') and line.strip()!='"""':
#continue
#elif line.strip().startswith("'''") and line.strip().endswith("'''") and line.strip()!="'''":
#continue
elif line.strip().startswith('#'):
continue
elif line.strip().startswith("'''") and flag == True:
flag = False
continue
elif line.strip().startswith('"""') and flag == True:
flag = False
continue
else:
line_count += 1
return line_count
def count_code_lines(path,file_types=[]):
# 判斷路徑是否存在
if not os.path.exists(path):
print("您輸入的目錄或文件路徑不存在")
return 0
line_count=0 #代碼行總數(shù)
file_lines_dict={} #每個(gè)文件代碼行數(shù)
# 判斷是否為文件
if os.path.isfile(path):
file_type = os.path.splitext(path)[1][1:] #取到文件后綴名
# 判斷文件類型是否滿足條件
if len(file_types)==0:
file_types=["py","cpp","c","java","ruby","ini","go","html","css","js","txt","vbs","php","asp","sh"]
if file_type in file_types:
line_count = count_file_lines(path)
return line_count
else:
file_path = []
for root, dirs, files in os.walk(path):
for file in files:
file_path.append(os.path.join(root,file))
for f in file_path:
file_type = os.path.splitext(f)[1][1:]
if len(file_types)==0:
file_types=
["py","cpp","c","java","ruby","ini","go","html","css","js","txt","vbs","php","asp","sh"]
if file_type not in file_types:
continue
line_num = count_file_lines(f)
line_count += line_num
file_lines_dict[f] = line_num
return line_count,file_lines_dict
if __name__=="__main__":
print (sys.argv)
if len(sys.argv) < 2:
print ("請(qǐng)輸入待統(tǒng)計(jì)行數(shù)的代碼絕對(duì)路徑!")
sys.exit()
count_path = sys.argv[1]
file_types = []
if len(sys.argv) >2:
for i in sys.argv[2:]:
file_types.append(i)
#print(count_path,file_types)
print(count_code_lines(count_path,file_types))
#print(count_file_lines("b.py"))
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python實(shí)現(xiàn)統(tǒng)計(jì)代碼行數(shù)的小工具
- python統(tǒng)計(jì)指定目錄內(nèi)文件的代碼行數(shù)
- python實(shí)現(xiàn)代碼統(tǒng)計(jì)器
- python實(shí)現(xiàn)代碼統(tǒng)計(jì)程序
- python tkinter圖形界面代碼統(tǒng)計(jì)工具(更新)
- python3使用GUI統(tǒng)計(jì)代碼量
- python tkinter圖形界面代碼統(tǒng)計(jì)工具
- 使用Python設(shè)計(jì)一個(gè)代碼統(tǒng)計(jì)工具
- Python實(shí)現(xiàn)統(tǒng)計(jì)代碼行的方法分析
- python 統(tǒng)計(jì)代碼行數(shù)簡(jiǎn)單實(shí)例
相關(guān)文章
Python錯(cuò)誤提示:[Errno 24] Too many open files的分析與解決
這篇文章主要給大家介紹了Python中出現(xiàn)錯(cuò)誤提示:[Errno 24] Too many open files的分析與解決,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-02-02
Python編程快速上手——Excel到CSV的轉(zhuǎn)換程序案例分析
這篇文章主要介紹了Python Excel到CSV的轉(zhuǎn)換程序,結(jié)合具體案例形式分析了Python操作Excel到CSV轉(zhuǎn)換的操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2020-02-02
深入理解Tensorflow中的masking和padding
TensorFlow 是一個(gè)用于人工智能的開(kāi)源神器,這篇文章主要介紹了Tensorflow中的masking和padding的相關(guān)知識(shí),通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
Python+DeOldify實(shí)現(xiàn)老照片上色功能
DeOldify是一種技術(shù),以彩色和恢復(fù)舊的黑白圖像,甚至電影片段。它是由一個(gè)叫Jason?Antic的人開(kāi)發(fā)和更新的。本文將利用DeOldify實(shí)現(xiàn)老照片上色功能,感興趣的可以了解一下2022-06-06
Django接受前端數(shù)據(jù)的幾種方法總結(jié)
下面小編就為大家?guī)?lái)一篇Django接受前端數(shù)據(jù)的幾種方法總結(jié)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11
Python編程根據(jù)字典列表相同鍵的值進(jìn)行合并
這篇文章主要介紹了來(lái)學(xué)習(xí)Python字典列表根據(jù)相同鍵的值進(jìn)行合并的操作方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10
windows下安裝Python虛擬環(huán)境virtualenvwrapper-win
這篇文章主要介紹了windows下安裝Python虛擬環(huán)境virtualenvwrapper-win,內(nèi)容超簡(jiǎn)單,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06

