Ниже коды использовались в Python 2 для объединения всех текстовых файлов в папке. Он работал нормально.
import os
base_folder = "C:\\FDD\\"
all_files = []
for each in os.listdir(base_folder):
if each.endswith('.txt'):
kk = os.path.join(base_folder, each)
all_files.append(kk)
with open(base_folder + "Combined.txt", 'w') as outfile:
for fname in all_files:
with open(fname) as infile:
for line in infile:
outfile.write(line)
Когда в Python 3 выдает ошибку:
Traceback (most recent call last):
File "C:\Scripts\thescript.py", line 26, in <module>
for line in infile:
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'CP_UTF8' codec can't decode byte 0xe4 in position 53: No mapping for the Unicode character exists in the target code page.
Я сделал это изменение:
with open(fname) as infile:
в
with open(fname, 'r', encoding = 'latin-1') as infile:
Это дает мне «MemoryError».
Как я могу исправить эту ошибку в Python 3? Спасибо.