Skip to content

Commit 9895a8b

Browse files
robot527cailurus
authored andcommitted
count all files
1 parent 11db6ec commit 9895a8b

1 file changed

Lines changed: 37 additions & 13 deletions

File tree

‎robot527/0007/count_codes.py‎

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def count_lines(self):
1616
if self.file_name[-3:] != '.py':
1717
print(self.file_name + ' is not a .py file!')
1818
return
19+
mc_flag = False
1920
try:
2021
with open(self.file_name) as code:
2122
for each_line in code:
@@ -25,27 +26,50 @@ def count_lines(self):
2526
self.blank_line_num += 1
2627
elif temp[0] == '#':
2728
self.comment_line_num += 1
28-
29+
else:
30+
if False == mc_flag:
31+
if temp[0:3] == '"""':
32+
mc_flag = True
33+
elif temp[-3:] == '"""':
34+
mc_flag = False
35+
self.comment_line_num += 1
36+
if mc_flag:
37+
self.comment_line_num += 1
2938
except IOError as err:
3039
print('File error: ' + str(err))
3140

41+
def display(self):
42+
print('-.' * 15)
43+
print('The ' + self.file_name + ' code statistic is:')
44+
print('Total line number is:', self.total_line_num)
45+
print('Blank line number is:', self.blank_line_num)
46+
print('Comment line number is:', self.comment_line_num)
47+
3248
import os
33-
target_path = '.'
49+
50+
target_path = '../../../python_primer/mrdw/cgi-bin'
51+
#Get file list from the target directory excluding files in its subdirectories
3452
file_list = [f for f in os.listdir(target_path)
3553
if os.path.isfile(os.path.join(target_path, f))]
36-
#print(file_list, len(file_list))
37-
54+
#Get .py file list
3855
pyfile_list = [os.path.join(target_path, f) for f in file_list
3956
if f[-3:] == '.py']
4057

41-
print(pyfile_list[0])
58+
Total_line_num = 0
59+
Blank_line_num = 0
60+
Comment_line_num = 0
4261

43-
pyf1 = PyfileInfo(pyfile_list[0])
44-
pyf1.count_lines()
45-
#pyf2 = PyfileInfo('test.c')
46-
#pyf2.count_lines()
62+
print('==' * 32)
63+
for each_file in pyfile_list:
64+
py_file = PyfileInfo(each_file)
65+
py_file.count_lines()
66+
py_file.display()
67+
Total_line_num += py_file.total_line_num
68+
Blank_line_num += py_file.blank_line_num
69+
Comment_line_num += py_file.comment_line_num
4770

48-
print('==' * 18)
49-
print('Total line number is:', pyf1.total_line_num)
50-
print('Blank line number is:', pyf1.blank_line_num)
51-
print('Comment line number is:', pyf1.comment_line_num)
71+
print('=-' * 24)
72+
print('All code files in ' + target_path + ' statistic is:')
73+
print('Total line number is:', Total_line_num)
74+
print('Blank line number is:', Blank_line_num)
75+
print('Comment line number is:', Comment_line_num)

0 commit comments

Comments
 (0)