File tree Expand file tree Collapse file tree 2 files changed +19
-4
lines changed Expand file tree Collapse file tree 2 files changed +19
-4
lines changed Original file line number Diff line number Diff line change 1
1
#!/bin/env python
2
2
# -*- coding: utf-8 -*-
3
3
4
- #完成了一部分,还没有找出空行和注释行数
4
+ #算是完成了,查找Python中的#开头的单行注释足够了,不过计算/**/的单行乃至多行注释还没有做到,也暂时不想写。
5
+ #在字符串(全文读入)或者字符串(分段读取)组成的list中计算出行数长度或者摘录出想要的部分内容是我下一步要思考的问题
6
+ #目前掌握的正则在单行情况下还比较理想,多行就比较吃力了,顺便想多自己写一些,不是不想用库,是有的库给出的方式不是我想要的,比如解析XML对于作为纯用户的直观感较差
5
7
6
8
import os
7
9
import glob
10
+ import re
8
11
9
12
def get_file_list ():
10
13
return glob .glob ('*' )
11
14
12
15
def file_read (filename ):
16
+ n2 = n3 = 0
13
17
with open (filename ,'r' ) as fp :
14
18
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
16
25
17
26
if __name__ == '__main__' :
18
- n1 = 0
27
+ n1 = n2 = n3 = 0
19
28
os .chdir ('./code' )
20
29
file_list = get_file_list ()
21
30
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 ]
23
34
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
+ #测试
1
2
void setup ()
2
3
{
3
4
size (800 ,800 );
You can’t perform that action at this time.
0 commit comments