您好,登錄后才能下訂單哦!
這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)如何在python3項目中使用GUI統(tǒng)計代碼量,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
具體內(nèi)容如下
# coding=utf-8 ''' 選擇一個路徑 遍歷路徑下的每一個文件,統(tǒng)計代碼量 字典存儲 每一種類型文件的代碼行數(shù),eg: *.py -> 行數(shù) 全局變量 總行數(shù) 需要注意的是,這里僅僅能打開utf-8編碼的文件,其他類型的文件無法打開,會出現(xiàn)解碼錯誤 解決方法:使用try-except語句,遇到解碼錯誤就跳過,即 except UnicodeDecodeError: ''' import easygui as g import sys import os # 全局變量 總行數(shù) total_line_num = 0 # 字典存儲 每一種類型文件的代碼行數(shù),eg: *.py -> 行數(shù) code_file_dict = {} def func1(file_path): if os.path.isdir(file_path): file_list = os.listdir(file_path) # 列出當前路徑下的全部內(nèi)容 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,則跳過,不統(tǒng)計 pass else: func1(path_plus) elif os.path.isfile(path_plus): try: with open(path_plus, 'r') as f: # 每個文件的代碼行數(shù) 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( '總行數(shù)為:{}\n已經(jīng)完成了{}%\n離十萬行代碼還差{}行'.format( total_line_num, (total_line_num / 100000) * 100, 100000 - total_line_num), title='統(tǒng)計結(jié)果', text=[ '{a}類型的代碼有行\(zhòng)n'.format(a=k,b=v) for k,v in code_file_dict.items()], codebox=1) except TypeError as reason: g.msgbox('取消了統(tǒng)計代碼行操作')
上述就是小編為大家分享的如何在python3項目中使用GUI統(tǒng)計代碼量了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。