-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount-lines.py
executable file
·40 lines (29 loc) · 1.16 KB
/
count-lines.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
34
35
36
37
38
39
40
#!/usr/bin/env python
## By Davoud Arsalani
## https://github.com/davoudarsalani/scripts
## https://github.com/davoudarsalani/scripts/blob/master/count-lines.py
## https://raw.githubusercontent.com/davoudarsalani/scripts/master/count-lines.py
## https://davoudarsalani.ir
from magic import Magic
from natsort import natsorted
from os import listdir, path
from re import match, compile as re_compile
from gp import Color, invalid
Col = Color()
text_reg = re_compile(r'^text/.+$')
total_lines = 0
text_files = natsorted([_ for _ in listdir() if path.isfile(_)])
if not text_files:
invalid('no text files')
for f in text_files:
mime = Magic(mime=True).from_file(f)
if match(text_reg, mime) or mime in ['application/json', 'application/csv', 'message/news']:
try:
with open(f) as opened:
content = opened.readlines()
length = len(content)
total_lines += length
print(f'{length:,}\t{f}')
except Exception as exc:
print(Col.red(f'?\t{f} (ERROR: {exc})')) ## {exc} instead of {exc!r} because error message may be too long
print(Col.yellow(f'\n{total_lines:,}\ttotal'))