[Python]复制文件时报错 UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xff in position 0

在尝试用脚本实现文件内容复制时,出现了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()

参考文章:

关于python内open函数encoding编码问题

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值