返回顶部

read,readline,readlines的特点与区别

1.read

  • 读取全部文件
with open("test.text", "r",encoding='utf8') as f:
    print(f.read())

输出:

2.readline

  • 读取下一行
with open("test.text", "r",encoding='utf8') as f:
    # print(f.read())
    for i in range(10):
        print(f.readline())

输出:

3.readlines

  • 读取全部,并返回一个列表
with open("test.text", "r",encoding='utf8') as f:
    print(f.readlines(), type(f.readlines()))

输出:

posted @ 2019-10-03 10:47  高薪程序员  阅读(291)  评论(0编辑  收藏  举报