7

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.

Suppose my file has only the following content.

This is the first time I am posing a question on this site, I will appreciate if someone clarifies my doubts for learning the Python. I thank the StackOverflow for this platform.

In the commands read(), readline() and readlines(), one difference is of course reading whole file, or a single line, or specified line.

But I didn't understand the use/necessity of bracket () in these commands. For example, what is the difference in readline() and readline(7)? If the argument 7 exceeds the number of lines in the file, what will be output?


On the web mentioned above, it is explained what the argument in read() does; but it is not mentioned what the argument in readline() or readlines() does?

Georgy
  • 12,464
  • 7
  • 65
  • 73
Soluble
  • 171
  • 1
  • 1
  • 4

2 Answers2

12
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.

Dilan
  • 2,610
  • 7
  • 23
  • 33
Ryan Breen
  • 121
  • 1
  • 4
2

For details, you should consult the library documentation, not the tutorial.

From io documentation:

readline(size=-1)

Read and return one line from the stream. If size is specified, at most size bytes will be read.

The line terminator is always b'\n' for binary files; for text files, the newline argument to open() can be used to select the line terminator(s) recognized.


readlines(hint=-1)

Read and return a list of lines from the stream. hint can be specified to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceeds hint.

Note that it’s already possible to iterate on file objects using for line in file: ... without calling file.readlines().

So, readline() reads an entire line. readline(7) reads at most 7 bytes of a line. readlines() reads all the lines as a list. readlines(7) returns at least 1 complete line and more lines as well( until it exceeds 7 bytes)

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • So *readline(7)* reads at most 7 bytes of a line; but *which line?* – Soluble Sep 24 '19 at 05:10
  • 1
    The line at the current position in the file. If you have just opened the file, the first line. If you have already read the first line, then the second line. If you jumped to 1000th byte using `file.seek`, then... whatever line contains the 1000th byte. – Amadan Sep 24 '19 at 05:12