115 of 151 menu

The readlines method

The readlines method reads a file and returns all its lines as a list. The optional parameter of the method can specify the number of bytes of the file that we need to read.

Syntax

file.readlines([number of bytes in a file])

Example

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

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

The result of the executed code:

['text\n', 'text\n', 'text']

See also

  • method readline,
    which reads the first line of the 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
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline