-
Notifications
You must be signed in to change notification settings - Fork 1
/
count.py
93 lines (80 loc) · 2.53 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
#该脚本用来统计文件夹下c语言文件的行数
# 说明:UTF兼容ISO8859-1和ASCII,GB18030兼容GBK,GBK兼容GB2312,GB2312兼容ASCII
CODES = ['UTF-8', 'GB18030', 'BIG5']
# UTF-8 BOM前缀字节
UTF_8_BOM = b'\xef\xbb\xbf'
def file_encoding(file_path: str):
"""
获取文件编码类型
:param file_path: 文件路径
:return:
"""
with open(file_path, 'rb') as f:
return string_encoding(f.read())
def string_encoding(data: bytes):
"""
获取字符编码类型
:param data: 字节数据
:return:
"""
# 遍历编码类型
for code in CODES:
try:
data.decode(encoding=code)
if 'UTF-8' == code and data.startswith(UTF_8_BOM):
return 'UTF-8-SIG'
return code
except UnicodeDecodeError:
continue
return 'unknown'
def findAllFilesWithSpecifiedSuffix(target_dir):
# print(target_dir)
find_res = []
# target_suffix_dot = "." + target_suffix
walk_generator = os.walk(target_dir)
for root_path, dir, files in walk_generator:
if len(files) < 1:
continue
for file in files:
# file_name, suffix_name = os.path.splitext(file)
# if suffix_name == target_suffix_dot:
find_res.append(os.path.join(root_path, file))
for pdir in dir:
if pdir=="." or pdir == '..':
# print(pdir)
continue
find_res+= findAllFilesWithSpecifiedSuffix(os.path.join(root_path,pdir))
return find_res
# 统计路径中的文件数量
def countLinesInFiles(files):
count =0
paths=set()
for filePath in files:
fileName,suffix=os.path.splitext(filePath)
# if suffix!='.c' and suffix!='.h' and suffix!='.gtg' and suffix!='.cmd' and suffix!='.md':
# continue
if suffix!='.c' and suffix!='.h':
continue
if fileName=="gtg":
continue
if filePath in paths:
continue
paths.add(filePath)
print(filePath,end="\t")
f=open(filePath,'r',encoding= file_encoding(filePath))
lines=f.readlines()
lines=list(filter(lambda x: x!='\n',lines))
# for str in lines:
# print(str)
# lines=list(filter(lambda x: ))
add=len(lines)
print(add,end='\t')
count+=add
print(count)
return count
# 首先从控制台获取系统路径
dir="."
files=findAllFilesWithSpecifiedSuffix(dir)
n=countLinesInFiles(files)
print(n)