user9799161
user9799161

Reputation: 125

Unicode decode error while iterating through all txt files in folder

I would like to write the expression | SYS into each single txt file in the folder. However I am getting the Unicode decode error. I have suspicion that it might be because of missing r in the string with open(txt_file, "r") as f:

My code is:

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)

And the error is:

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>

Upvotes: 0

Views: 151

Answers (2)

user9799161
user9799161

Reputation: 125

Although it is not safest with most files, I solved it by adding ignore error in the line (txt_file, "r") as f:, making it (txt_file, errors='ignore') as f:.

Upvotes: 0

Jdizzle
Jdizzle

Reputation: 96

You could try to set the encoding argument when calling open():

with open(txt_file, "r", encoding="utf-8") as f:

Upvotes: 1

Related Questions