File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change 1+ # prints recursive count of lines of python source code from current directory
2+ # includes an ignore_list. also prints total sloc
3+
4+ import os
5+ cur_path = os .getcwd ()
6+ ignore_set = set (["__init__.py" , "count_sourcelines.py" ])
7+
8+ loc_list = []
9+
10+ for py_dir , _ , py_files in os .walk (cur_path ):
11+ for py_file in py_files :
12+ if py_file .endswith (".py" ) and py_file not in ignore_set :
13+ total_path = os .path .join (py_dir , py_file )
14+ try :
15+ # Open the file with UTF-8 encoding
16+ with open (total_path , "r" , encoding = "utf-8" ) as file :
17+ loc_list .append ((len (file .read ().splitlines ()),
18+ total_path .split (cur_path )[1 ]))
19+ except UnicodeDecodeError as e :
20+ print (f"Skipping file { total_path } due to encoding error: { e } " )
21+
22+ for line_number_count , filename in loc_list :
23+ print ("%05d lines in %s" % (line_number_count , filename ))
24+
25+ print ("\n Total: %s lines (%s)" % (sum ([x [0 ] for x in loc_list ]), cur_path ))
You can’t perform that action at this time.
0 commit comments