from sys import argv
script,inputfile=argv
def print_all(file):
print(file.read())
def rewind(file):
file.seek(0)
def print_line(line,file):
print(line,file.readline())
currentFile=open(inputfile)
print("Let's print the first line: \n")
print_all(currentFile)
print("Rewind")
rewind(currentFile)
currentLine=1
print_line(currentLine,currentFile)
currentLine+=1
print_line(currentLine,currentFile)
currentLine+=1
print_line(currentLine,currentFile)
我有这段代码,这个工程,但我不明白的是,当我重写打印语句在打印行函数打印(file.readline(line))我得到意想不到的输出。我使用Python 3.6python readline没有给出预期的输出
正确的输出
This is line 1
This is line 2
This is line 3
Rewind
This is line 1
This is line 2
This is line 3
不正确的输出
This is line 1
This is line 2
This is line 3
Rewind
T
hi
s i
为什么会发生这种情况?
你能告诉我们该文件的内容。 –
无法用包含这三行的示例文件重现。也许你的输入文件有文本编辑器无法识别的换行符?尝试'print(repr(file.read()))' – poke
这是行1 这是行2 这是行3 –