Tag: Class Leo using the OOB run return value file div Refresh
Overview
The write () method of the Python file is used to write the specified string to the file.
The contents of the string are stored in the buffer before the file is closed or before the buffer is flushed, and you cannot see what is written in the file.
Grammar
The write () method syntax is as follows:
Fileobject.write (str)
Parameters
return value
The method has no return value.
Instance
The contents of the file Runoob.txt are as follows:
1:www.runoob.com2:www.runoob.com3:www.runoob.com4:www.runoob.com5:www.runoob.com
The following example demonstrates the use of the Write () method:
#!/usr/bin/python3# Open File fo = open ("Runoob.txt", "r+", encoding= "Utf-8") print ("File name:", Fo.name) # Write a line at the end of the file Fo.seek (0,2) Fo.write ("\n6:www.runoob.com") # reads all the contents of the file Fo.seek (0,0) for index in range (6): line = Next (FO) print ("File row number%d-%s" % (index, line)) # Close file Fo.close ()
The result of the above example output is:
File line number 0-1:www.runoob.com file line number 1-2:www.runoob.com file line number 2-3:www.runoob.com file line number 3-4:www.runoob.com file line number 4-5:www.runoob. COM file line number 5-6:www.runoob.com
To view the contents of a file:
$ cat Runoob.txt 1:www.runoob.com2:www.runoob.com3:www.runoob.com4:www.runoob.com5:www.runoob.com6:www.runoob.com
Python file Write () method