获取错误编解码器无法对位置 8-13 中的字符进行编码:字符映射到 <undefined>

Getting Error codec can't encode characters in position 8-13: character maps to <undefined>

我收到这个错误

Traceback (most recent call last):
  File "C:\Users\Anthony\PycharmProjects\ReadFile\main.py", line 14, in <module>
    masterFile.write("Line {}: {}\n".format(index, line.strip()))
  File "C:\Users\Anthony\AppData\Local\Programs\Python\Python39\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 8-13: character maps to undefined

该程序应该搜索目录中的所有 txt,并在其中搜索特定的单词。一旦找到它,它就会将它们打印到带有该行的文件中,然后还打印带有完整行号的文件的另一个副本。将有大约 100 个 txt 文件,它会在我收到此错误消息之前处理前 3 个。所有文件都是 UTF-8 编码的。我试着改变 with open(file, encoding="utf-8") as f: 但是没用。

import glob

searchWord = "Hello"
dataFile = open("C:/Users/Anthony/Documents/TextDataFolder/TextData.txt", 'w')
masterFile = open("C:/Users/Anthony/Documents/TextDataFolder/masterFile.txt", 'w')

files = glob.iglob("#C:/Users/Anthony/Documents/Texts/*.txt", recursive = True)
for file in files:
    with open(file) as f:  
        print(file)
        for index, line in enumerate(f):
             #print("Line {}: {}".format(index, line.strip()))
             masterFile.write("Line {}: {}\n".format(index, line.strip()))
             if searchWord in line:
                print("Line {}: {}".format(index, line.strip()))
                dataFile.write("Line {}: {}\n".format(index, line.strip()))

我终于弄明白了……我觉得自己像个白痴。问题不是我对文件的阅读。这是我的作品没有编码。只是试图编码我的阅读。所以最终看起来像这样

import glob

searchWord = "Hello"
dataFile = open("C:/Users/Anthony/Documents/TextDataFolder/TextData.txt", 'w', encoding="utf-8")masterFile = masterFile = open("C:/Users/Anthony/Documents/TextDataFolder/masterFile.txt", 'w', encoding="utf-8")

files = glob.iglob("#C:/Users/Anthony/Documents/Texts/*.txt", recursive = True)
for file in files:
    with open(file, "r", encoding="utf-8") as f:  
        print(file)
        for index, line in enumerate(f):
             #print("Line {}: {}".format(index, line.strip()))
             masterFile.write("Line {}: {}\n".format(index, line.strip()))
             if searchWord in line:
                print("Line {}: {}".format(index, line.strip()))
                dataFile.write("Line {}: {}\n".format(index, line.strip()))