Repository navigation
Expand file tree
/
Copy pathcount_codes.py
More file actions
executable file
·91 lines (82 loc) · 3.45 KB
/
Copy pathcount_codes.py
File metadata and controls
executable file
·91 lines (82 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#! usr/bin/python3
"""
第 0007 题:有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。
包括空行和注释,但是要分别列出来。
"""
class PyfileInfo:
def __init__(self, file):
self.file_name = file
self.total_line_num = 0
self.blank_line_num = 0
self.comment_line_num = 0
def count_lines(self):
if self.file_name[-3:] != '.py':
print(self.file_name + ' is not a .py file!')
return
mc_flag = False
mc_start = ''
"""
最高优先级检测由三连引号包围的多行注释,也可能是单行注释;
其次检测空行;
再次检测由#开头的单行注释,由单引号或者双引号包围的单行注释;
"""
try:
with open(self.file_name) as code:
for each_line in code:
self.total_line_num += 1
temp = each_line.strip()
if mc_flag is False:
if temp[0:3] in ["'''", '"""']:
mc_start = temp[0:3]
mc_flag = True
if len(temp) >= 6 and temp[-3:] == mc_start:
self.comment_line_num += 1
mc_start = ''
mc_flag = False
else:
if len(temp) == 0:
self.blank_line_num += 1
else:
if temp[0] == '#':
self.comment_line_num += 1
elif temp[0] in ['"', "'"] and temp[0] == temp[-1:]:
self.comment_line_num += 1
elif temp[-3:] == mc_start:
#found the end of the multiple-line comment
self.comment_line_num += 1
mc_start = ''
mc_flag = False
if mc_flag:
self.comment_line_num += 1
except IOError as err:
print('File error: ' + str(err))
def display(self):
print('-.' * 15)
print('The ' + self.file_name + ' code statistic is:')
print('Total line number is:', self.total_line_num)
print('Blank line number is:', self.blank_line_num)
print('Comment line number is:', self.comment_line_num)
import os
target_path = '../../../python_primer/mrdw/cgi-bin'
#Get file list from the target directory excluding files in its subdirectories
file_list = [f for f in os.listdir(target_path)
if os.path.isfile(os.path.join(target_path, f))]
#Get .py file list
pyfile_list = [os.path.join(target_path, f) for f in file_list
if f[-3:] == '.py']
Total_line_num = 0
Blank_line_num = 0
Comment_line_num = 0
print('==' * 32)
for each_file in pyfile_list:
py_file = PyfileInfo(each_file)
py_file.count_lines()
py_file.display()
Total_line_num += py_file.total_line_num
Blank_line_num += py_file.blank_line_num
Comment_line_num += py_file.comment_line_num
print('=-' * 24)
print('All code files in ' + target_path + ' statistic is:')
print('Total line number is:', Total_line_num)
print('Blank line number is:', Blank_line_num)
print('Comment line number is:', Comment_line_num)