file = open("filename.txt", "r")
print(file.readlines())
file.close()
结果
>>>
['Line 1 text \n', 'Line 2 text \n', 'Line 3 text']
>>>
您还可以使用for循环来迭代文件中的行:
file = open("filename.txt", "r")
for line in file:
print(line)
file.close()
例子
>>>
Line 1 text
Line 2 text
Line 3 text
>>>