https://pypi.python.org/pypi/chardet
import chardet
#以rb读取文件返回文件的编码(用到了chardet类)
with open(file_name, 'rb') as f:
raw = f.read()
result = chardet.detect(raw)
encoding = result['encoding']
lines = 0
with open(file_name,encoding=encoding) as f:
print('正在分析文件:%s ...' % file_name)
try:
for each_line in f:
lines += 1
except Exception as reason:
print(str(reason)) # 读取出错显示错误信息......
print('%s -> %s' % (file_name,lines))
return lines
- import os
- import chardet
- m = []
- x = []
- temp = input('请将该代码放在要查找的文件夹内,请输入关键子:')
- for each in os.walk('E:/Json60r8'):
- m.append(each)
- y = len(m)
- while True:
- if y != (-1):
- for each in m[y-1][2]:
- x.append(m[y-1][0]+'/'+each)
- else:
- break
- y-=1
- for each in x:
- #以rb读取文件返回文件的编码(用到了chardet类)
- with open(each, 'rb') as f:
- raw = f.read()
- result = chardet.detect(raw)
- encoding = result['encoding']
-
- r = open(each, encoding=encoding)
- try:
- for i in r:
- if '曹植' in i:
- print(i)
- else:
- print('没找到')
- except Exception as reason:
- print(str(reason)) # 读取出错显示错误信息......
-
复制代码