python3使用GUI統(tǒng)計代碼量
更新時間:2019年09月18日 15:54:53 作者:jasonLee_lijiaqi
這篇文章主要為大家詳細介紹了python3使用GUI統(tǒng)計代碼量,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python3使用GUI統(tǒng)計代碼量的具體代碼,供大家參考,具體內(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)計結果',
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)計代碼行操作')
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關文章
python web應用程序之Django數(shù)據(jù)庫詳解
這篇文章主要介紹了python web應用程序之Django數(shù)據(jù)庫,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2024-06-06
詳解Python下Flask-ApScheduler快速指南
Flask是Python社區(qū)非常流行的一個Web開發(fā)框架,本文將嘗試將介紹APScheduler應用于Flask之中,非常具有實用價值,需要的朋友可以參考下2018-11-11
python實現(xiàn)調(diào)用攝像頭并拍照發(fā)郵箱
這篇文章主要介紹了python實現(xiàn)調(diào)用攝像頭并拍照發(fā)郵箱的程序,幫助大家更好的理解和學習使用python,感興趣的朋友可以了解下2021-04-04
Python+?Flask實現(xiàn)Mock?Server詳情
這篇文章主要介紹了Python+?Flask實現(xiàn)Mock?Server詳情,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09

