File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11#!/bin/env python
22# -*- coding: utf-8 -*-
33
4- #完成了一部分,还没有找出空行和注释行数
4+ #算是完成了,查找Python中的#开头的单行注释足够了,不过计算/**/的单行乃至多行注释还没有做到,也暂时不想写。
5+ #在字符串(全文读入)或者字符串(分段读取)组成的list中计算出行数长度或者摘录出想要的部分内容是我下一步要思考的问题
6+ #目前掌握的正则在单行情况下还比较理想,多行就比较吃力了,顺便想多自己写一些,不是不想用库,是有的库给出的方式不是我想要的,比如解析XML对于作为纯用户的直观感较差
57
68import os
79import glob
10+ import re
811
912def get_file_list ():
1013 return glob .glob ('*' )
1114
1215def file_read (filename ):
16+ n2 = n3 = 0
1317 with open (filename ,'r' ) as fp :
1418 linelist = fp .readlines ()
15- return len (linelist )
19+ for string in linelist :
20+ if re .match (r'#' , string ) != None :
21+ n2 += 1
22+ elif re .match (r'^ *\n' , string ) != None :
23+ n3 += 1
24+ return len (linelist ),n2 ,n3
1625
1726if __name__ == '__main__' :
18- n1 = 0
27+ n1 = n2 = n3 = 0
1928 os .chdir ('./code' )
2029 file_list = get_file_list ()
2130 for name in file_list :
22- n1 += file_read (name )
31+ n1 += file_read (name )[0 ]
32+ n2 += file_read (name )[1 ]
33+ n3 += file_read (name )[2 ]
2334 print '文件夹中的文件总行数为' + str (n1 ) + '行'
35+ print '文件夹中的注释总行数为' + str (n2 ) + '行'
36+ print '文件夹中的空行总行数为' + str (n3 ) + '行'
37+ print '文件夹中的代码总行数为' + str (n1 - n2 - n3 ) + '行'
Original file line number Diff line number Diff line change 1+ #测试
12void setup ()
23{
34size (800 ,800 );
You can’t perform that action at this time.
0 commit comments