116 of 151 menu

The write method

The write method writes a file and returns an integer representing the number of bytes written. In the method parameter, we specify the string that we need to write to the file.

Syntax

file.write(what we write)

Example

Let's write the string 'text1' to the file file.txt, and then read its contents:

res = open('file1.txt', 'w') # open file for writing print(res.write('text1')) res = open('file1.txt', 'r') # open file for reading print(res.read())

The result of the executed code:

5 text1

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