编写Python程序实现行数统计

开发 后端
我们在这里编写的Python程序主要是实现对代码行数的统计。希望初学者们可以通过本文介绍的内容,加深对这一语言的认知程度。

当我们在使用Python编程语言进行程序开发的时候,会发现这一功能强大的语言可以给我们带来非常大的作用。那么,接下来我们将会通过一个Python程序的实现,来仔细分析一下这语言给我们带来的独特之处。#t#

正好一直在关注Python,还没有用Python写过程序,今天就利用中午休息的时间写了一个简单的Python程序用于代码统计。对输入的路径作递归,查找代码文件,对每一个代码文件计算它的注释行数,空行数,真正的代码行数。自己用的程序,就写的粗糙了,也没加异常处理。

主要的Python程序脚本文件LineCount.py的内容如下:

  1. import sys;  
  2. import os;  
  3. class LineCount:  
  4. def trim(self,docstring):  
  5. if not docstring:  
  6. return ''  
  7. lines = docstring.expandtabs().splitlines()  
  8. indent = sys.maxint  
  9. for line in lines[1:]:  
  10. stripped = line.lstrip()  
  11. if stripped:  
  12. indent = min(indent, len(line) - len(stripped))  
  13. trimmed = [lines[0].strip()]  
  14. if indent < sys.maxint: 
  15. for line in lines[1:]:  
  16. trimmed.append(line[indent:].rstrip())  
  17. while trimmed and not trimmed[-1]:  
  18. trimmed.pop()  
  19. while trimmed and not trimmed[0]:  
  20. trimmed.pop(0)  
  21. return '\n'.join(trimmed)  
  22. def FileLineCount(self,filename):  
  23. (filepath,tempfilename) = os.path.split(filename);  
  24. (shotname,extension) = os.path.splitext(tempfilename);  
  25. if extension == '.txt' or extension == '.hol' : # file type   
  26. file = open(filename,'r');  
  27. self.sourceFileCount += 1;  
  28. allLines = file.readlines();  
  29. file.close();  
  30. lineCount =0;  
  31. commentCount = 0;  
  32. blankCount = 0;  
  33. codeCount = 0;  
  34. for eachLine in allLines:  
  35. if eachLine != " " :  
  36. eachLineeachLine = eachLine.replace(" ",""); #remove space  
  37. eachLine = self.trim(eachLine); #remove tabIndent  
  38. if eachLine.find('--') == 0 : #LINECOMMENT   
  39. commentCount += 1;  
  40. else :  
  41. if eachLine == "":  
  42. blankCount += 1;  
  43. else :  
  44. codeCount += 1;  
  45. lineCountlineCount = lineCount + 1;  
  46. self.all += lineCount;  
  47. self.allComment += commentCount;  
  48. self.allBlank += blankCount;  
  49. self.allSource += codeCount;  
  50. print filename;  
  51. print ' Total :',lineCount ;  
  52. print ' Comment :',commentCount;  
  53. print ' Blank :',blankCount;  
  54. print ' Source :',codeCount;  
  55. def CalulateCodeCount(self,filename):  
  56. if os.path.isdir(filename) :  
  57. if not filename.endswith('\\'):  
  58. filename += '\\';   
  59. for file in os.listdir(filename):  
  60. if os.path.isdir(filename + file):  
  61. self.CalulateCodeCount(filename + file);  
  62. else:  
  63. self.FileLineCount(filename + file);  
  64. else:  
  65. self.FileLineCount(filename);  
  66. # Open File  
  67. def __init__(self):  
  68. self.all = 0;  
  69. self.allComment =0;  
  70. self.allBlank = 0;  
  71. self.allSource = 0;  
  72. self.sourceFileCount = 0;  
  73. filename = raw_input('Enter file name: ');  
  74. self.CalulateCodeCount(filename);  
  75. if self.sourceFileCount == 0 :  
  76. print 'No Code File';  
  77. pass;  
  78. print '\n';  
  79. print '***************** All Files **********************';  
  80. print ' Files :',self.sourceFileCount;  
  81. print ' Total :',self.all;  
  82. print ' Comment :',self.allComment;  
  83. print ' Blank :',self.allBlank;  
  84. print ' Source :',self.allSource;  
  85. print '****************************************************';  
  86. myLineCount = LineCount(); 

可以看到extension == '.txt' or extension == '.hol'这句是判断文件的后缀,来确定是否要计算代码行数。if eachLine.find('--') == 0 :这句来判断当前行是不是单行注释(我们的这门语言不支持块注释)。为了能在其他机器上运行,使用了py2exe来把Python脚本生成可执行的exe,setup.py脚本内容如下:

  1. from distutils.core import setup  
  2. import py2exe  
  3. setup(  
  4. version = "0.0.1",  
  5. description = "LineCount",  
  6. name = "LineCount",  
  7. console = ["LineCount.py"],  

 

不过生成exe后程序臃肿很多,有3M多。感觉使用Python程序确实是件很惬意的事。

责任编辑:曹凯 来源: 博客园
相关推荐

2019-08-01 15:08:37

PythonLine操作系统

2015-07-22 12:42:36

Pivot行列转换

2011-06-16 10:09:25

QT Windows DLL

2022-06-24 09:58:35

大数据JavaPython

2020-12-14 13:24:17

PandasSQL数据集

2011-06-27 13:57:42

JavaScript

2009-12-08 18:01:00

曙光移动集中采购

2012-09-13 10:44:18

Python代码

2010-02-03 09:27:21

编写Python程序

2022-11-17 10:23:13

VS CodeCodiumPython

2017-11-20 14:46:27

命令代码

2010-03-04 09:49:58

Python Hell

2017-05-08 10:38:36

PythonJavaScriptWIFI

2010-03-04 15:45:56

Python程序调试

2010-03-09 10:49:35

python简单应用

2011-03-02 15:35:15

Oracle分组统计

2018-03-30 10:26:24

行间距行高iOS

2011-11-08 10:13:20

2011-06-16 17:54:30

Qt Mplayer

2015-05-08 13:09:12

JavaScriipt抽奖程序
点赞
收藏

51CTO技术栈公众号