Search code examples
pythonpathlib

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?


Solution

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

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