因为x中的行遍历每一行.
with open('test.txt') as x:
for line in x:
# By this point, line is set to the first line
# the file cursor has advanced just past the first line
print(x.read())
# the above prints everything after the first line
# file cursor reaches EOF, no more lines to iterate in for loop
也许你的意思是:
with open('test.txt') as x:
print(x.read())
一次打印,或:
with open('test.txt') as x:
for line in x:
print line.rstrip()
逐行打印.建议使用后者,因为您不需要立即将文件的全部内容加载到内存中.