在尝试用脚本实现文件内容复制时,出现了Unicode解码错误。
错误:
PS E:\自学\python\project> echo "This is a test file." > test.txt
PS E:\自学\python\project> cat .\test.txt
This is a test file.
PS E:\自学\python\project> python .\ex17.py .\test.txt .\new_file.txt
Copying from .\test.txt to .\new_file.txt
Traceback (most recent call last):
File "E:\自学\python\project\ex17.py", line 10, in <module>
indata = in_file.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 0: illegal multibyte sequence
原因分析:
根据错误提示我们可以看出是由‘gbk’编码器无法正常解码导致的。这时我们打开刚刚创建好的文件(test.txt)时发现,它的编码方式是UTF-16。
于是修改代码中打开test.txt的函数,修改解码方式,使其为“UTF-16”,并再次运行
in_file = open(from_file,encoding="UTF-16")
运行结果:
PS E:\自学\python\project> python .\ex17.py .\test.txt .\new_file.txt
Copying from .\test.txt to .\new_file.txt
The input file is 21 bytes long
Does the output file exist? True
Ready, hit RETURN to continue, CRTL-C to abort.
Alright, all done.
PS E:\自学\python\project> cat .\new_file.txt
This is a test file.
文件内容成功复制,问题解决了。
结论:
这是由于编码不同导致的错误,python的内建函数open()的完整格式是:
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
encoding表示的是返回的数据采用何种编码,默认情况下,它采用的编码方式为gbk,或UTF-8。
而在命令行中使用echo创建.txt文件时,编码默认为UTF-16。
故我们在获取文件内容时应保持一致,固定encoding为UTF-16。
复制文件脚本:
from sys import argv
from os.path import exists
script, from_file, to_file = argv # pylint: disable=unbalanced-tuple-unpacking
print(f"Copying from {from_file} to {to_file}")
# we could do these two on one line, how?
in_file = open(from_file,encoding="UTF-16")
indata = in_file.read()
print(f"The input file is {len(indata)} bytes long")
print(f"Does the output file exist? {exists(to_file)}")
print("Ready, hit RETURN to continue, CRTL-C to abort.")
input()
out_file = open(to_file,'w')
out_file.write(indata)
print("Alright, all done.")
out_file.close()
in_file.close()