Python file Write () method

Source: Internet
Author: User

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
    • Str --the string to write to the file.

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.