代碼如下:
#-*-coding:utf-8-*-
from sys import argv
script, from_file, to_file =argv
print(open(from_file).read())
input('>')
open(to_file,'w').write(open(from_file, encoding='utf-8', errors='ignore').read()) #open文件后要記得close保存,如果open后,調用read了,可以不用額外調用close,read已經保存文件
print(open(to_file).read())
報錯如下:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 0: illegal multibyte sequence
原因及解決辦法:
1.首先,打開的文件不是gbk編碼,所以解碼報錯
2.嘗試了以下辦法解決:
#-*-coding:utf-8-*-
from sys import argv
script, from_file, to_file =argv
print(open(from_file, encoding='utf-8', errors='ignore').read())
input('>')
open(to_file,'w').write(open(from_file, encoding='utf-8', errors='ignore').read()) #open文件后要記得close保存,如果open后,調用read了,可以不用額外調用close,read已經保存文件
print(open(to_file).read())
但會出現如下問題:
一句話的中每個單詞的字母間都會有空格:

3.最終可以從根本上解決問題的方法:將需要open的文件用Notepad++打開,使用utf-8 編碼,則完美解決

