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
39 lines (38 loc) · 1.72 KB
/
0007.py
File metadata and controls
39 lines (38 loc) · 1.72 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
'''
第 0007 题: 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
'''
import os.path
import re
def mainKeywords(dirPath):
blank, comments, codelines, totalines, count, temp = 0, 0, 0, 0, 0, 0
f_list = os.listdir(dirPath)
for i in f_list:
if os.path.splitext(i)[1] == '.py':
print(i)
with open(i, 'r', encoding='utf-8') as fp:
while True:
line = fp.readline()
totalines += 1
if not line:
break
elif line.strip().startswith('#'):
comments += 1
elif line.strip().startswith("'''") or line.strip().startswith('"""'):
comments += 1
if line.count('"""') == 1 or line.count("'''") == 1:
while True:
line = fp.readline()
totalines += 1
comments += 1
if ("'''" in line) or ('"""' in line):
break
elif line.strip():
codelines += 1
else:
blank += 1
print('the nuber of totalines is : ' + str(totalines-1))
print('the nuber of comments is : ' + str(comments))
print('the nuber of codelines is : ' + str(codelines))
print('the nuber of blanklines is : ' + str(blank))
blank, comments, codelines, totalines = 0, 0, 0, 0
mainKeywords('D:\PyCharm 2017.1.3\projects')