The readline method

The readline method reads a file and returns its first line. The optional parameter of the method can be used to specify the number of bytes of the file that we need to read.

Syntax

pythonfile.readline([number of bytes in a file])

Example

Let's open the file file.txt using the open function, and then read its first line:

pythonres = open('file.txt', 'r')
print(res.readline())

The result of the executed code:

pythontext

See also

  • method readlines,
    which reads all lines of a file
  • method read,
    which reads the file
  • function open,
    which opens the file
  • method write,
    which writes a string to a file
  • method close,
    which closes the file