偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Python學(xué)習(xí)教程:如何用Python統(tǒng)計代碼行數(shù)

開發(fā) 后端
改良后的代碼可以對python和C系列的代碼實行行數(shù)計算,包括代碼、空行和注釋行,用re抓取注釋,傳入一個目錄自動對其下的文件進(jìn)行讀取計算

 Python學(xué)習(xí)教程:如何用python統(tǒng)計代碼行數(shù)

改良后的代碼可以對python和C系列的代碼實行行數(shù)計算,包括代碼、空行和注釋行,用re抓取注釋,傳入一個目錄自動對其下的文件進(jìn)行讀取計算。

[[272529]]

流程

首先判斷傳入?yún)?shù)是否為文件夾,不是則打印出提示,否則繼續(xù)(無返回),獲得目錄后,yongos.listdir對路徑下文件進(jìn)行遍歷,其中也包含文件夾,再次判斷是否為文件夾,是的話則遞歸調(diào)用此函數(shù),否則開始執(zhí)行行數(shù)統(tǒng)計,這里用os.path.join將路徑與文件名進(jìn)行拼接,方便之后直接傳給函數(shù),邏輯很簡單,無非是執(zhí)行文件判斷,判斷是哪類文件,在調(diào)用對應(yīng)的注釋監(jiān)測正則代碼段進(jìn)行抓取,抓取到則行數(shù)+1,空白行也是一樣的原理,用strip(去除前后空格),然后行內(nèi)內(nèi)容為空則為空行,代碼段即為總行數(shù)減去其他兩類行數(shù),最后在外層將所有文件對應(yīng)的代碼段累加即為total。

關(guān)鍵

函數(shù)內(nèi)部是可以訪問全局變量的,問題在于函數(shù)內(nèi)部修改了變量,導(dǎo)致python認(rèn)為它是一個局部變量。

所以,如果在函數(shù)內(nèi)部訪問并修改全局變量,應(yīng)該使用關(guān)鍵字 global 來修飾變量。

  1. import os 
  2. import re 
  3. #定義規(guī)則抓取文件中的python注釋 
  4. re_obj_py = re.compile('[(#)]'
  5. #定義規(guī)則抓取文件中的C語言注釋 
  6. re_obj_c = re.compile('[(//)(/*)(*)(*/)]'
  7. #判斷是否為python文件 
  8. def is_py_file(filename): 
  9. if os.path.splitext(filename)[1] == '.py'
  10. return True 
  11. else
  12. return False 
  13. #判斷是否為c文件 
  14. def is_c_file(filename): 
  15. if os.path.splitext(filename)[1] in ['.c''.cc''.h']: 
  16. return True 
  17. else
  18. return False 
  19. #定義幾個全局變量用于計算所有文件總和(全部行數(shù)、代碼行數(shù)、空行數(shù)、注釋行數(shù)) 
  20. all_lines, code_lines, space_lines, comments_lines = 0, 0, 0, 0 
  21. #判斷是否為文件夾,不是則輸出提示 
  22. def count_codelines(dirpath): 
  23. if not os.path.isdir(dirpath): 
  24. print('input dir: %s is not legal!' % dirpath) 
  25. return 
  26. # 定義幾個全局變量用于計算每個文件行數(shù)(全部行數(shù)、代碼行數(shù)、空行數(shù)、注釋行數(shù)) 
  27. global all_lines, code_lines, space_lines, comments_lines 
  28. #列出當(dāng)前文件夾下的文件(包含目錄) 
  29. all_files = os.listdir(dirpath) 
  30. for file in all_files: 
  31. #將文件(目錄)名與路徑拼接 
  32. file_name = os.path.join(dirpath, file) 
  33. if os.path.isdir(file_name): 
  34. count_codelines(file_name) 
  35. else
  36. temp_all_lines, temp_code_lines, temp_space_lines, temp_comments_lines = 0, 0, 0, 0 
  37. f = open(file_name) 
  38. for line in f: 
  39. temp_all_lines += 1 
  40. if line.strip() == ''
  41. temp_space_lines += 1 
  42. continue 
  43. if is_py_file(file_name) and re_obj_py.match(line.strip()): 
  44. temp_comments_lines += 1 
  45. if is_c_file(file_name) and re_obj_c.match(line.strip()): 
  46. temp_comments_lines += 1 
  47. temp_code_lines = temp_all_lines - temp_space_lines - temp_comments_lines 
  48. print('%-15s : all_lines(%s)\t code_lines(%s)\t space_lines(%s)\t comments_lines(%s)' 
  49. % (file, temp_all_lines, temp_code_lines, temp_space_lines, temp_comments_lines)) 
  50. all_lines += temp_all_lines 
  51. code_lines += temp_code_lines 
  52. space_lines += temp_space_lines 
  53. comments_lines += temp_comments_lines 
  54. if __name__ == '__main__'
  55. count_codelines('test'
  56. print('\n**** TOTAL COUNT ****\nall_lines = %s\ncode_lines = %s\nspace_lines = %s\ncomments_lines = %s' % (all_lines, code_lines, space_lines, comments_lines)) 

本期的Python學(xué)習(xí)教程先跟大家分享這么多!

責(zé)任編輯:華軒 來源: 今日頭條
相關(guān)推薦

2025-05-29 02:15:00

統(tǒng)計代碼行數(shù)

2019-01-15 14:21:13

Python數(shù)據(jù)分析數(shù)據(jù)

2018-05-17 10:05:24

運行iPadPython

2010-03-04 15:24:14

Python程序

2015-08-10 11:09:09

Python代碼Python

2024-11-12 07:36:39

Python編程數(shù)據(jù)挖掘

2021-08-08 22:08:41

Redis開發(fā)網(wǎng)頁

2018-03-27 18:12:12

PythonHTML

2020-07-10 09:49:53

數(shù)據(jù)清理數(shù)據(jù)分析查找異常

2023-02-08 07:09:40

PythonChatGPT語言模型

2024-01-03 10:15:59

Python函數(shù)

2020-09-10 11:20:37

Python機(jī)器學(xué)習(xí)人工智能

2024-09-23 10:00:00

代碼Python

2019-11-28 09:23:17

Python機(jī)器學(xué)習(xí)數(shù)據(jù)庫

2020-05-09 10:38:31

Python透視表數(shù)據(jù)

2020-12-10 10:46:23

PythonExcel圖片

2020-04-10 12:25:28

Python爬蟲代碼

2017-07-07 09:21:53

Python 機(jī)器學(xué)習(xí)決策

2022-11-02 14:45:24

Python數(shù)據(jù)分析工具

2022-06-24 09:58:35

大數(shù)據(jù)JavaPython
點贊
收藏

51CTO技術(shù)棧公眾號