user4807488
user4807488

Reputation:

UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 2433: character maps to <undefined>

I'm using the following code to write a text file to a variable

from pathlib import Path
paragraph = Path('myfile.txt', encoding='utf-8').read_text()

I'm getting the following error

Traceback (most recent call last):
  File "Z:\python\projects\vb\test.py", line 4, in <module>
    paragraph = Path('myfile.txt', encoding='utf-8').read_text()
  File "C:\Users\zagg\AppData\Local\Programs\Python\Python310\lib\pathlib.py", line 1133, in read_text
    return f.read()
  File "C:\Users\zagg\AppData\Local\Programs\Python\Python310\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 0x9d in position 2470: character maps to <undefined>

I see similar posts with the same error, but none of the answers worked for me. any suggestions?

Upvotes: 1

Views: 8808

Answers (2)

Ashan Priyadarshana
Ashan Priyadarshana

Reputation: 3629

Just add encoding='utf-8' within .read_text():

paragraph = Path('myfile.txt').read_text(encoding='utf-8')

Upvotes: 1

Tomerikoo
Tomerikoo

Reputation: 19432

Interestingly, Path doesn't define an __init__ and its __new__ gracefully ignores any **kwargs passed to it. So the encoding='utf-8' in Path('myfile.txt', encoding='utf-8') basically has no effect whatsoever.

On the other hand, read_text does indeed accepts an encoding argument.

Upvotes: 0

Related Questions