我正在尝试使用函数显示文本文件的内容但是我收到错误我不知道该怎么办。我正在使用Python 3.2。
代码:
from sys import argv
from os.path import exists
script, input_file = argv
def print_all(foo):
print(foo.read())
def rewind(foo):
print("rewinding...")
foo.seek(0)
def print_line(n, foo):
print(n, foo.readline())
temp = open(input_file)
print_all(temp)
rewind(temp)
print_line(1, temp)
print_line(2, temp)
错误报告:
PS C:\Python32> python Sample1.py sample2.txt
Traceback (most recent call last):
File "Sample1.py", line 18, in <module>
print_all(temp)
File "Sample1.py", line 7, in print_all
print(foo.read())
File "C:\Python32\lib\encodings\cp437.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\xfe' in position 1: character maps to <undefined>
文本文件sample2.txt的内容仅为英文字母。