forked from Show-Me-the-Code/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0007.py
More file actions
35 lines (31 loc) · 1.15 KB
/
0007.py
File metadata and controls
35 lines (31 loc) · 1.15 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
# -*- coding: utf-8 -*-
"""
**第 0007 题:**
有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
"""
import os
import re
def stat_code(dir_path):
if not os.path.isdir(dir_path):
return
exp_re = re.compile(r'^#.*')
file_list = os.listdir(dir_path)
print("%s\t%s\t%s\t%s" % ('file', 'all_lines', 'space_lines', 'exp_lines'))
for file in file_list:
file_path = os.path.join(dir_path, file)
if os.path.isfile(file_path) and os.path.splitext(file_path)[1] == '.py':
with open(file_path) as f:
all_lines = 0
space_lines = 0
exp_lines = 0
for line in f.readlines():
all_lines += 1
if line.strip() == '':
space_lines += 1
continue
exp = exp_re.findall(line.strip())
if exp:
exp_lines += 1
print("%s\t%s\t%s\t%s" % (file, all_lines, space_lines, exp_lines))
if __name__ == '__main__':
stat_code('.')