這期內容當中小編將會給大家帶來有關如何在python3項目中使用GUI統計代碼量,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
具體內容如下
# coding=utf-8
'''
選擇一個路徑
遍歷路徑下的每一個文件,統計代碼量
字典存儲 每一種類型文件的代碼行數,eg: *.py -> 行數
全局變量 總行數
需要注意的是,這里僅僅能打開utf-8編碼的文件,其他類型的文件無法打開,會出現解碼錯誤
解決方法:使用try-except語句,遇到解碼錯誤就跳過,即 except UnicodeDecodeError:
'''
import easygui as g
import sys
import os
# 全局變量 總行數
total_line_num = 0
# 字典存儲 每一種類型文件的代碼行數,eg: *.py -> 行數
code_file_dict = {}
def func1(file_path):
if os.path.isdir(file_path):
file_list = os.listdir(file_path) # 列出當前路徑下的全部內容
for each in file_list:
path_plus = file_path + os.sep + each
if os.path.isdir(path_plus):
if os.path.basename(path_plus) in [
'venv', '.idea']: # 如果目錄為venv或者.idea,則跳過,不統計
pass
else:
func1(path_plus)
elif os.path.isfile(path_plus):
try:
with open(path_plus, 'r') as f:
# 每個文件的代碼行數
line_num = 0
for eachline in f:
global total_line_num # 聲明全局變量
total_line_num += 1
line_num += 1
'''
將each分割出后綴名,存儲在字典中
'''
(temp_path, temp_name) = os.path.basename(each).split('.')
temp = '.' + temp_name
global code_file_dict
if temp not in code_file_dict:
code_file_dict[temp] = line_num
else:
code_file_dict[temp] += line_num
except UnicodeDecodeError:
pass
else:
g.msgbox('該路徑只是一個文件', '提示')
sys.exit(0)
if __name__ == '__main__':
try:
dir = g.diropenbox('請選擇的你的代碼庫', '瀏覽文件夾', default='.')
func1(dir)
print(code_file_dict)
g.textbox(
'總行數為:{}\n已經完成了{}%\n離十萬行代碼還差{}行'.format(
total_line_num,
(total_line_num / 100000) * 100,
100000 - total_line_num),
title='統計結果',
text=[
'{a}類型的代碼有行\n'.format(a=k,b=v) for k,v in code_file_dict.items()],
codebox=1)
except TypeError as reason:
g.msgbox('取消了統計代碼行操作')上述就是小編為大家分享的如何在python3項目中使用GUI統計代碼量了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。