运行 "Learn Python the Hard Way" 里的代码时出现的报错。代码:
1. from sys import argv
2. from os.path import exists
3.
4. script, from_file, to_file = argv
5.
6. print(f"Copying from {from_file} to {to_file}")
7.
8. # we could do these two on one line, how?
9. in_file = open(from_file)
10. indata = in_file.read()
11.
12. print(f"The input file is {len(indata)} bytes long")
13.
14. print(f"Does the output file exsit? {exists(to_file)}")
15. print("Ready, hit RETURN to continue, CTRL-C to abort.")
16. input()
17.
18. out_file = open(to_file, 'w')
19. out_file.write(indata)
20.
21. print("Alright, all done.")
22.
23. out_file.close()
24. in_file.close()
照抄书本在 Terminal 上输入的命令:
$ echo "This is a test file." > test.txt
$ cat test.txt
This is a test file.
$ python ex17.py test.txt new_file.txt
下面是 Terminal 给的提示:
Copying from test.txt to new_file.txt
Traback ( most recent call last):
File "ex17.py", line 10, in
indata = in_file.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 0: illegal multibyte sequence
尝试把 'gbk' 编码改为 'UTF-8':
9. in_file = open(from_file, 'r', encoding = 'UTF-8')
Terminal 给出的提示
Copying from test.txt to new_file.txt
Traceback (most recent call last):
File "ex17.py", line 10, in
indata = in_file.read()
File "C:\Python36\lib\codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
求解:是什么导致这个 Error ?以及如何解决?