How do I fix this cp950 "illegal multibyte sequence" UnicodeDecodeError when reading a text file?


Keywords:python 


Question: 

My teacher teach us that how to use "exec",but I got an error:

UnicodeDecodeError: 'cp950' codec can't decode byte 0xe6 in position 1814: illegal multibyte sequence

I use:

exec(open("somefile.py").read())

how to fix this problem?


1 Answer: 

Given this is presumably Python 3 source code, the likely encoding is UTF-8 (it's the standard encoding for Python 3 source code).

If that's the case, changing open("somefile.py") to open("somefile.py", encoding="utf-8") would specify the encoding explicitly, overriding the locale default, which should allow you to read it in correctly.

For idiomatic code, you'd also want to use a with statement (to guarantee deterministic closing of the file), making it:

with open("somefile.py", encoding="utf-8") as f:
    exec(f.read())