forked from luozhaohui/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountline.py
More file actions
executable file
·55 lines (45 loc) · 1.32 KB
/
countline.py
File metadata and controls
executable file
·55 lines (45 loc) · 1.32 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
#! encoding=utf-8
#
# countline.py
# counting lines of java/python file or java/python files in a dir
#
# Python Version: Python 3.4.3
#
import os
import codecs
def process_file(path):
total = 0
if path.endswith('.java') or path.endswith('.py'):
with codecs.open(path, 'r', 'utf-8') as handle:
for line in handle.readlines():
# print(line)
line = line.lstrip()
if len(line) > 1:
if not line.startswith("//") and not line.startswith("#"):
total += 1
#print("%s has %d lines"%(path, total))
return total
def process_dir(path):
total = 0
for filename in os.listdir(path):
filepath = os.path.join(path, filename)
if os.path.isdir(filepath):
# exclude hidden dirs
if filename.startswith("."):
pass
else:
total += process_dir(filepath)
elif os.path.isfile(filepath):
total += process_file(filepath)
return total
def process(path):
total = 0
if os.path.isdir(path):
total = process_dir(path)
elif os.path.isfile(path):
total = process_file(path)
print('>>> total code lines : %d' % total)
return total
process('countline.py')
# process('matplot')