Python 遍历文件夹中的所有txt文件时出现Unicode解码错误
我想将表达式Python 遍历文件夹中的所有txt文件时出现Unicode解码错误,python,python-3.x,Python,Python 3.x,我想将表达式|SYS写入文件夹中的每个txt文件中。然而,我得到了Unicode解码错误。我怀疑这可能是因为字符串中缺少r,而open(txt_文件,“r”)作为f: 我的代码是: import os import csv import glob cwd = os.getcwd() directory = cwd output = cwd txt_files = os.path.join(directory, '*.txt') for txt_file in glob.glob(txt
|SYS
写入文件夹中的每个txt文件中。然而,我得到了Unicode解码错误。我怀疑这可能是因为字符串中缺少r
,而open(txt_文件,“r”)作为f:
我的代码是:
import os
import csv
import glob
cwd = os.getcwd()
directory = cwd
output = cwd
txt_files = os.path.join(directory, '*.txt')
for txt_file in glob.glob(txt_files):
with open(txt_file, "r") as f:
a = f.read()
print(a)
#Now writing into the file with the prepend line + old file data
with open(txt_file, "w") as f:
f.write("| SYS" + a)
#below code to verify the data in the file
with open(txt_file, "r") as f:
b = f.read()
print(b)
错误是:
Traceback (most recent call last):
File "C:/Users/xxxxxx/Downloads/TEST2/Searchcombine.py", line 15, in <module>
a = f.read()
File "C:\Python\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 1060662: character maps to <undefined>
回溯(最近一次呼叫最后一次):
文件“C:/Users/xxxxxx/Downloads/TEST2/Searchcombine.py”,第15行,在
a=f.read()
文件“C:\Python\lib\encodings\cp1252.py”,第23行,解码
返回编解码器.charmap\u解码(输入、自身错误、解码表)[0]
UnicodeDecodeError:“charmap”编解码器无法解码位置1060662:字符映射到的字节0x8f
调用open()时,可以尝试设置编码参数:
虽然对大多数文件来说这不是最安全的,但我通过在
(txt_文件,“r”)行中添加ignoreerror
作为f:
,使其(txt_文件,errors='ignore')作为f:
仍然相同:回溯(最近一次调用最后一次):文件“C:/Users/xxxxxxx/Downloads/TEST2/Searchcombine.py”,第15行,在a=f.read()文件“C:\Python\lib\codecs.py”,第322行,在decode(result,consumered)=self.\u buffer\u decode(data,self.errors,final)UnicodeDecodeError:“utf-8”编解码器无法对192位的字节0xfc进行解码:无效的起始字节
@user9799161如果不是utf-8,请使用模块查找文件编码,打开文件时使用正确的编码。还有其他想法吗?以二进制(模式rb/wb)打开文件,显然你不关心文件的内容没有任何意义,因此向现有垃圾中添加更多垃圾并不是一个巨大的改变。
with open(txt_file, "r", encoding="utf-8") as f: