Python read-write file write and flush

Source: Internet
Author: User

Open the file with open, the function creates a file object that will be used to invoke other support methods associated with it.

Object= Open([, access_mode] [, buffering])     

The following is the details of the parameters:

    • The File_name:file_name parameter is a string value that contains the name of the file that you want to access.

    • Access_mode:access_mode decides that the file must be opened, i.e., read, write, append, etc. the possible values are given in the table below for a complete list of patterns. This is an optional parameter and the default file access mode is read (R).

    • Buffering: If the buffer value is set to 0 o'clock, no buffering will occur. If the buffer value is 1, the row buffer is accessed by a file to execute. If the specified buffer value is an integer greater than 1, the buffering action is made with the size of the indicated buffer. If negative, the size of the buffer is the system default (the default behavior).

This is the open file?? List of different modes:

Mode Description
R Open a file as read-only. The file pointer is placed at the beginning of the file. This is the default mode.
Rb Opening a file can only be read in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
r+ Open for reading and writing files. The file pointer will be at the beginning of the file.
rb+ Opens a file for reading and writing binary formats. The file pointer will be at the beginning of the file.
W Open a file to write only. Overwrite the file if the file exists. If the file does not exist, it is created to write a new file.
Wb Opening a file can only be written in binary format. Overwrite the file if the file exists. If the file does not exist, it is created to write a new file.
w+ Open the file for writing and reading. Overwrite the existing file if the file exists. If the file does not exist, a new file is created to read and write.
wb+ Opens a file for writing and reading in binary format. Overwrite the existing file if the file exists. If the file does not exist, a new file is created to read and write.
A The Append file opens. The file pointer is at the end of the file. That is, the file is in attach mode. If the file does not exist, it creates a new file to write to.
Ab The appended binary format file opens. The file pointer ends at the end of the file. In other words, the file is in append mode. If the file does not exist, it creates and writes a new file.
A + Open to append and read files. The file pointer ends at the end of the file. The file will be in append mode. If the file does not exist, it creates and reads and writes a new file.
ab+ Open two append and read binary format files. The file pointer ends at the end of the file. The file will be in append mode. If the file does not exist, it creates and reads and writes a new file.

Close files with close:

The close () method of a file object flushes the information that is not written, and closes the object of the file, after which no data content can be written.

Write to file with write:

The write () method writes a string to any open file. Note that a Python string can have binary data, not just text.

Read files with read:

The Read () method reads a string of open files. Note that a Python string can have binary data, not just text.

It is important to note that because of buffering, the string may not actually appear in the file until the call to flush () or the close () method is called.

Generally, file stream operations contain buffering mechanisms, and the Write method does not write data directly to a file, but rather writes the memory to a specific buffer first.

The Flush method is used to flush the buffer, and the data in the buffer is immediately written to the file while emptying the buffer.

Normally, when the buffer is full, the operating system automatically writes buffered data to the file.

As for the Close method, the principle is to call the Flush method internally to flush the buffer, and then perform the close operation, so that the data integrity is guaranteed even if the buffer data is not full.

If the process exits unexpectedly or exits gracefully, the file's Close method is not executed, and the contents of the buffer are lost.

Python read-write file write and flush

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.