window環(huán)境下,python讀寫磁盤目錄下的文件。
- 內(nèi)容是中文:
微撲克是騰訊爸爸旗下的一款產(chǎn)品,目前僅支持微信登錄,
游戲?qū)ο笠矁H針對(duì)于好友(即沒(méi)有在線隨機(jī)進(jìn)去其
他不認(rèn)識(shí)的網(wǎng)友開(kāi)的房間)下面來(lái)分析一下,
它是如何在眾多線上德?lián)漕惍a(chǎn)品中脫穎而出的吧。
- 代碼部分
# encoding = utf-8
data = open("d:\\Users\\Administrator\\PycharmProjects\\untitled1\\exception_dir\\sketch.txt", "r")
print(data.readline(), end="")
- 出現(xiàn)異常
Traceback (most recent call last):
File "D:/Users/Administrator/PycharmProjects/untitled1/exception_dir/read_file.py", line 5, in <module>
print(data.readline(), end="")
UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 2: illegal multibyte sequence
- 中文默認(rèn)的編碼是gbk格式,這里讀寫要指定open的編碼格式為utf-8,代碼修改成:
# encoding = utf-8
data = open("d:\\Users\\Administrator\\PycharmProjects\\untitled1\\exception_dir\\sketch.txt",
mode='r', encoding='utf-8')
print(data.readline(), end="")
- 這次正常讀寫,done
"C:\Program Files (x86)\Python36-32\python.exe" D:/Users/Administrator/PycharmProjects/untitled1/exception_dir/read_file.py
微撲克是騰訊爸爸旗下的一款產(chǎn)品,目前僅支持微信登錄,
Process finished with exit code 0