Difference in read(), readline() and readlines() in Python

后端 未结 2 565
[愿得一人]
[愿得一人] 2020-12-12 04:45

I was looking on a web of Python the commands mentioned in title and their difference; however, I have not satisfied with a complete basic understanding of these commands.

2条回答
  •  无人及你
    2020-12-12 05:18

    read(n)
    filevar.read()
    

    Reads and returns a string of n characters, or the entire file as a single string if n is not provided.

    readline(n)
    filevar.readline()
    

    Returns the next line of the file with all text up to and including the newline character. If n is provided as a parameter than only n characters will be returned if the line is longer than n.

    readlines(n)
    filevar.readlines()
    

    Returns a list of strings, each representing a single line of the file. If n is not provided then all lines of the file are returned. If n is provided then n characters are read but n is rounded up so that an entire line is returned.

提交回复
热议问题