感谢莫烦老师
详情
file= open(‘my file.txt‘,‘r‘)
content=file.read()
print(content)
""""
This is my first test.
This is the second line.
This the third line.
This is appended file.
""""
file= open(‘my file.txt‘,‘r‘)
content=file.readline() # 读取第一行
print(content)
""""
This is my first test.
""""
second_read_time=file.readline() # 读取第二行
print(second_read_time)
"""
This is the second line.
"""
file= open(‘my file.txt‘,‘r‘)
content=file.readlines() # python_list 形式
print(content)
""""
[‘This is my first test.\n‘, ‘This is the second line.\n‘, ‘This the third line.\n‘, ‘This is appended file.‘]
""""
# 之后如果使用 for 来迭代输出:
for item in content:
print(item)
"""
This is my first test.
This is the second line.
This the third line.
This is appended file.
"""
原文:https://www.cnblogs.com/cloud-ken/p/12634130.html