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調(diào)用pyttsx3實(shí)現(xiàn)離線文字轉(zhuǎn)語音的方式
pyttsx3是 Python 中的文本到語音的離線轉(zhuǎn)換庫,本文給大家介紹Python調(diào)用pyttsx3實(shí)現(xiàn)離線文字轉(zhuǎn)語音的方式,感興趣的朋友一起看看吧2024-03-03
python讀取.mat文件及將變量存為.mat文件的詳細(xì)介紹
這篇文章主要給大家介紹了關(guān)于python讀取.mat文件及將變量存為.mat文件的詳細(xì)介紹,?mat文件是matlab的數(shù)據(jù)存儲(chǔ)的標(biāo)準(zhǔn)格式,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06
詳解Python?NumPy如何使用argsort方法進(jìn)行排序
NumPy提供了各種功能強(qiáng)大的數(shù)組操作方法,其中之一就是argsort方法,本文將詳細(xì)介紹argsort方法的使用,以及如何在實(shí)際項(xiàng)目中充分利用它進(jìn)行排序操作,希望對(duì)大家有所幫助2024-03-03
對(duì)Python中class和instance以及self的用法詳解
今天小編就為大家分享一篇對(duì)Python中class和instance以及self的用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06

