-
Notifications
You must be signed in to change notification settings - Fork 2
/
line-count.py
33 lines (29 loc) · 993 Bytes
/
line-count.py
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
import pathlib
class LoC(object):
suffixes = ['.py']
skip = ['name of dir or file to skip', ...]
def count(self, path, init=True):
path = pathlib.Path(path)
if path.name in self.skip:
print(f'skipped: {path.relative_to(self.root)}')
return
if init:
self.root = path
self.files = 0
self.lines = 0
if path.is_dir():
# recursive case
for item in path.iterdir():
self.count(path=item, init=False)
elif path.is_file() and path.suffix in self.suffixes:
# base case
with path.open(mode='r') as f:
line_count = len(f.readlines())
print(f'{path.relative_to(self.root)}: {line_count}')
self.files += 1
self.lines += line_count
if init:
print(f'\n{self.lines} lines in {self.files} files')
if __name__ == '__main__':
loc = LoC()
loc.count('.')