python實(shí)現(xiàn)代碼統(tǒng)計(jì)程序
本文實(shí)例為大家分享了python實(shí)現(xiàn)代碼統(tǒng)計(jì)程序的具體代碼,供大家參考,具體內(nèi)容如下
# encoding="utf-8"
"""
統(tǒng)計(jì)代碼行數(shù)
"""
import sys
import os
def count_file_line(path):
"""統(tǒng)計(jì)文件的有效行數(shù)"""
countLine = 0
# 設(shè)置一個(gè)標(biāo)志位,當(dāng)遇到以"""或者'''開頭或者結(jié)尾的時(shí)候,置為False
flag = True
# 使用utf-8格式的編碼方式讀取文件,如果讀取失敗,將使用gbk編碼方式讀取文件
try:
fp = open(path, "r", encoding="utf-8")
encoding_type = "utf-8"
fp.close()
except:
encoding_type = "gbk"
with open(path, "r", encoding=encoding_type) as fp:
for line in fp:
# 空行不統(tǒng)計(jì)
if line.strip():
line = line.strip()
# 注意下面的這兩個(gè)elif必須要前面,這樣子當(dāng)('"""')結(jié)束之后及時(shí)將flag置為True
if line.endswith('"""') and flag == False:
flag = True
continue
if line.endswith("'''") and flag == False:
flag = True
continue
if flag == False:
continue
if line.startswith("#!") or line.startswith("#-*-") or line.startswith("# encoding"):
countLine += 1
# 如果以“#”號開頭的,不統(tǒng)計(jì)
elif line.startswith("#"):
continue
# 如果同時(shí)以("'''")或者('"""')開頭或者結(jié)尾(比如:"""aaa"""),那么不統(tǒng)計(jì)
elif line.startswith('"""') and line.endswith('"""') and line != '"""':
continue
elif line.startswith("'''") and line.endswith("'''") and line != "'''":
continue
# 如果以("'''")或者('"""')開頭或者結(jié)尾(比如:aaa"""或者"""bbb),那么不統(tǒng)計(jì)
# 注意下面的這兩個(gè)elif必須要放后面
elif line.startswith('"""') and flag == True:
flag = False
continue
elif line.startswith("'''") and flag == True:
flag = False
continue
else:
countLine += 1
return countLine
def count_codes(path,file_types=[]):
"""統(tǒng)計(jì)所有文件代碼行"""
# 判斷path是目錄還是文件,如果是目錄的話,遍歷目錄下所有的文件
if not os.path.exists(path):
print("您輸入的路徑不存在!")
return 0
countTotalLine = 0
file_paths = {}
if os.path.isdir(path):
for root,dirs,files in os.walk(path):
for name in files:
if not file_types:
file_types = ["txt","py"]
# print(file_types)
if os.path.splitext(name)[1][1:] in file_types:
file_path = os.path.normpath(os.path.join(root,name))
# print(file_path)
file_lines = count_file_line(file_path)
countTotalLine += file_lines
file_paths[file_path] = file_lines
else:
if not file_types:
file_types = ["txt","py"]
if os.path.splitext(path)[1][1:] in file_types:
countTotalLine = count_file_line(path)
file_paths[path] = count_file_line(path)
return countTotalLine,file_paths
if __name__ == "__main__":
# 打印出命令行輸入的參數(shù)
# print(sys.argv)
if len(sys.argv) < 2:
print("請輸入路徑!")
sys.exit()
path = sys.argv[1]
# print(path)
file_types = sys.argv[2:]
# print(file_types)
print(count_codes(path,file_types))
以上就是本文的全部內(nèi)容,希望對大家的學(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 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ù)簡單實(shí)例
- Python實(shí)現(xiàn)代碼統(tǒng)計(jì)工具
相關(guān)文章
對python PLT中的image和skimage處理圖片方法詳解
今天小編就為大家分享一篇對python PLT中的image和skimage處理圖片方法詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
python密碼學(xué)實(shí)現(xiàn)文件加密教程
這篇文章主要為大家介紹了python密碼學(xué)實(shí)現(xiàn)文件加密教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
pytorch LayerNorm參數(shù)的用法及計(jì)算過程
這篇文章主要介紹了pytorch LayerNorm參數(shù)的用法及計(jì)算過程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
Python獲取基金網(wǎng)站網(wǎng)頁內(nèi)容、使用BeautifulSoup庫分析html操作示例
這篇文章主要介紹了Python獲取基金網(wǎng)站網(wǎng)頁內(nèi)容、使用BeautifulSoup庫分析html操作,結(jié)合實(shí)例形式分析了Python基于urllib包的網(wǎng)頁內(nèi)容獲取,以及使用BeautifulSoup分析html相關(guān)操作技巧,需要的朋友可以參考下2019-06-06
python實(shí)現(xiàn)linux服務(wù)器批量修改密碼并生成execl
這篇文章主要介紹了python實(shí)現(xiàn)linux服務(wù)器批量修改密碼并生成execl示例,需要的朋友可以參考下2014-04-04
Django提示mysql版本過低:django.db.utils.NotSupportedError: My
這篇文章主要介紹了Django提示mysql版本過低:django.db.utils.NotSupportedError: MySQL 8 or later is required (found 5.7.26).的解決方法,文中有詳細(xì)的解決方案,具有一定的參考價(jià)值,需要的朋友可以參考下2024-03-03

