每次读取文件出现不同编码内容,头大。
对应每次文本编码格式不同的读取
bytes = min(32, os.path.getsize(file_path))
raw = open(file_path, 'rb').read(bytes)
result = chardet.detect(raw)
encoding = result['encoding']
f = open(file_path, "r", encoding=encoding)
f_content = f.readlines()
部分文件还是无法正确识别编码
更新ing
# 首先二进制方式打开文件
with open(absPath, 'rb') as frb:
# 检测编码方式
cur_encoding = chardet.detect(frb.read())['encoding']
# 指定文件编码方式
with open(absPath, 'r', encoding=cur_encoding) as fr:
Content = fr.read()
用这个包可以解决编码读取问题
linux中文文件读取,打开时候指定编码
os.scandir(path.encode('utf-8'))
获取文件是二进制文件名
file.decode('utf-8')
读取文件名需要解码,可以获得中文文件名