日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python學(xué)習(xí)教程:如何用Python統(tǒng)計代碼行數(shù)

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

獲嘉ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!

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

流程

首先判斷傳入?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í)教程先跟大家分享這么多!


當(dāng)前題目:Python學(xué)習(xí)教程:如何用Python統(tǒng)計代碼行數(shù)
網(wǎng)站URL:http://m.5511xx.com/article/dpshijg.html