Python中文件无法显示
>>> f = open('E:\\27text.txt')
>>> f.read(56)
Traceback (most recent call last):
File "", line 1, in
f.read(56)
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 44: illegal multibyte sequence
>>> f.read(5)
''
>>> f.read(10)
''
>>> f.read()
''
我直接迷惑,我特意把文件放到E盘里,不知道为什么显示不出来
编码不正确
1.先打开txt,另存为,把编码格式改为utf8
2.open方法后面加参数,encoding='utf8'
编码一致才能读出来
open('E:\27text.txt', encoding = 'utf-8')指定打开的编码格式为utf-8
请看👉 :python刚写入的文件无法读取的问题