Skip to content

Commit 1a8644c

Browse files
author
XiaomingSu
committed
count lines
1 parent b2124df commit 1a8644c

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

PyBeaner/0007/code_lines.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# coding=utf-8
2+
__author__ = 'PyBeaner'
3+
import os
4+
import fnmatch
5+
6+
total_lines = 0
7+
code_lines = 0
8+
empty_lines = 0
9+
comment_lines = 0
10+
11+
12+
def count_line(line):
13+
line = line.lstrip()
14+
global comment_lines, empty_lines, total_lines, code_lines
15+
16+
total_lines += 1
17+
if line.startswith("#"):
18+
comment_lines += 1
19+
elif not line:
20+
empty_lines += 1
21+
else:
22+
code_lines += 1
23+
24+
25+
def scan_dir(directory, suffix="*.py"):
26+
directory = os.path.abspath(directory)
27+
print("Scanning files in %s ..." % directory)
28+
for cur_dir, dirs, files in os.walk(directory):
29+
for file in files:
30+
if not fnmatch.fnmatch(file, suffix):
31+
continue
32+
file_path = os.path.join(cur_dir, file)
33+
with open(file_path,errors="replace") as f:
34+
for line in f:
35+
count_line(line)
36+
37+
38+
if __name__ == '__main__':
39+
scan_dir(r"../..")
40+
print("Total lines:%d" % total_lines)
41+
print("Code lines:%d" % code_lines)
42+
print("Empty lines:%d" % empty_lines)
43+
print("Comment lines:%d" % comment_lines)

0 commit comments

Comments
 (0)