欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實現(xiàn)代碼統(tǒng)計工具

 更新時間:2019年09月19日 09:57:44   作者:abclmnxyz  
這篇文章主要為大家詳細介紹了Python實現(xiàn)代碼統(tǒng)計工具,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Python實現(xiàn)代碼統(tǒng)計工具的具體代碼,供大家參考,具體內(nèi)容如下

思路:首先獲取所有文件,然后統(tǒng)計每個文件中代碼的行數(shù),最后將行數(shù)相加.

實現(xiàn)的功能:

統(tǒng)計每個文件的行數(shù);

統(tǒng)計總行數(shù);

支持指定統(tǒng)計文件類型,排除不想統(tǒng)計的文件類型;

排除空行;

排除注釋行

import os
import sys
import os.path
#for i in sys.argv:
# print (i)

# 判斷單個文件的代碼行數(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={} #每個文件代碼行數(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 ("請輸入待統(tǒng)計行數(shù)的代碼絕對路徑!")
 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"))

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python如何實現(xiàn)最小矩形覆蓋問題

    python如何實現(xiàn)最小矩形覆蓋問題

    這篇文章主要介紹了python如何實現(xiàn)最小矩形覆蓋問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Python調(diào)用pyttsx3實現(xiàn)離線文字轉(zhuǎn)語音的方式

    Python調(diào)用pyttsx3實現(xiàn)離線文字轉(zhuǎn)語音的方式

    pyttsx3是 Python 中的文本到語音的離線轉(zhuǎn)換庫,本文給大家介紹Python調(diào)用pyttsx3實現(xiàn)離線文字轉(zhuǎn)語音的方式,感興趣的朋友一起看看吧
    2024-03-03
  • Python assert語句的簡單使用示例

    Python assert語句的簡單使用示例

    這篇文章主要給大家介紹了關(guān)于Python assert語句的簡單使用,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Python具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-07-07
  • 如何利用Python監(jiān)控別人的網(wǎng)站

    如何利用Python監(jiān)控別人的網(wǎng)站

    這篇文章主要為大家詳細介紹了如何利用Python實現(xiàn)監(jiān)控別人的網(wǎng)站,這樣還可以詳細了解你的競爭對手網(wǎng)站,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-05-05
  • python3讀取csv文件任意行列代碼實例

    python3讀取csv文件任意行列代碼實例

    這篇文章主要介紹了python3讀取csv文件任意行列代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-01-01
  • python多線程與多進程及其區(qū)別詳解

    python多線程與多進程及其區(qū)別詳解

    這篇文章主要介紹了python多線程與多進程及其區(qū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-08-08
  • Python?NumPy矩陣對象詳解及方法

    Python?NumPy矩陣對象詳解及方法

    這篇文章主要介紹了Python?NumPy矩陣對象詳解及方法,文章圍繞主題展開詳細的內(nèi)容戒殺,具有一定的參考價值,需要的朋友可以參考一下
    2022-09-09
  • python讀取.mat文件及將變量存為.mat文件的詳細介紹

    python讀取.mat文件及將變量存為.mat文件的詳細介紹

    這篇文章主要給大家介紹了關(guān)于python讀取.mat文件及將變量存為.mat文件的詳細介紹,?mat文件是matlab的數(shù)據(jù)存儲的標準格式,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-06-06
  • 詳解Python?NumPy如何使用argsort方法進行排序

    詳解Python?NumPy如何使用argsort方法進行排序

    NumPy提供了各種功能強大的數(shù)組操作方法,其中之一就是argsort方法,本文將詳細介紹argsort方法的使用,以及如何在實際項目中充分利用它進行排序操作,希望對大家有所幫助
    2024-03-03
  • 對Python中class和instance以及self的用法詳解

    對Python中class和instance以及self的用法詳解

    今天小編就為大家分享一篇對Python中class和instance以及self的用法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06

最新評論