File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 )
You can’t perform that action at this time.
0 commit comments