Python文件访问编码格式问题UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position xx: 解决方案

1、Python读取文件

# 1、打开文件
file = open("ReadMe")
# 2、读取文件类容
text = file.read()
print(text)
# 3、关闭文件
file.close()

2、出现错误为

Traceback (most recent call last):
  File "F:/学习区/Python/13_文件/hm_01_读取文件内容.py", line 4, in <module>
    text = file.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 41: illegal multibyte sequence

3、修改文件打开时编码格式,

# 1、打开文件
file = open("ReadMe",encoding="UTF-8")

4、解决后

hello python
错误总会被慢慢解决
原文地址:https://www.cnblogs.com/karrya/p/10870572.html