Python file Write () method summary and function analysis (instance)

Source: Internet
Author: User
Let's look at this article today. Pythonwrite (), I'll explain it in this article. the Write method in Python as well as the Pythonwrite property , and it can be used in Python programming where to explain. No more nonsense, let's get started.

Overview

The Write () method 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.

If file open mode with B, then write the contents of the file, str (parameter) to use the Encode method to bytes form, otherwise error: typeerror:a Bytes-like object is required, not ' str '.

Grammar

The write () method syntax is as follows:

Fileobject.write ([str])

Parameters

STR--The string to write to the file.

return value

Returns the length of the character being written.

Instance

The following example demonstrates the use of the Write () method:

#!/usr/bin/python#-*-coding:utf-8-*-# Open File fo = open ("Test.txt", "W") print "file name:", fo.namestr = "Happy every Day" fo.write (str) # Close File Fo.close ()

The result of the above example output is:

File name is:  test.txt

To view the contents of a file:

$ cat test.txt Happy every day

In this article, we explain what is the write () method, do not understand the words can be hands-on to try, after all, hands-on practice is the best way to verify the learning. Finally, I hope that this article will give you a little help in learning about Python.

For more information, please visit the PHP Chinese Web Python tutorial section.

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.