117 of 151 menu

The close method

The close method closes a previously opened file to free up system resources. The method does not return anything. We do not specify anything in the method parameter.

Syntax

file.close()

Example

Let's open the file file.txt, read its contents, and then close it:

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

The result of the executed code:

text

See also

  • function open,
    which opens the file
  • method read,
    which reads the file
  • method readline,
    which reads the first line of the file
  • method readlines,
    which reads all lines of a file
  • method write,
    which writes a string to a 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