Python (9) IO programming-file read/write,

Source: Internet
Author: User

Python (9) IO programming-file read/write,
Python file read/write

Python has built-in functions for reading and writing files. Its usage is compatible with C. This section describes how to open/close a file, read/write a file object, and so on.

 

Open () close () with open (...)...

Take the following example to understand the open () and close () functions of Python. The read () method can be called to read all the content of the file at a time. Python reads the content to the memory and uses a str object. For details, see the following.

Create a.txt in the python_file folder of drive E. Enter the following information as needed:

      

The method for enabling and disabling Python operations is as follows:

      

Note that close () is required after open (). However, an IOError may occur during file read/write operations. To ensure that the file is properly closed no matter whether an error occurs, try... finally:

      

 

Python simplifies the rewrite method, that is, using with open (...) as...; it is recommended that this method be used for file read and write:

    

 

You must have noticed the parameter "r". This parameter determines the file opening mode: Read-only, write, and append. For all values, see the complete list below. This parameter is not mandatory. The default file access mode is read-only (r ).

Mode Description
R Open the file in read-only mode. The file pointer will be placed at the beginning of the file. This is the default mode.
Rb Open a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode.
R + Open a file for reading and writing. The file pointer will be placed at the beginning of the file.
Rb + Open a file in binary format for reading and writing. The file pointer will be placed at the beginning of the file.
W Open a file for writing only. If the file already exists, overwrite it. If the file does not exist, create a new file.
Wb Open a file in binary format for writing only. If the file already exists, overwrite it. If the file does not exist, create a new file.
W + Open a file for reading and writing. If the file already exists, overwrite it. If the file does not exist, create a new file.
Wb + Open a file in binary format for reading and writing. If the file already exists, overwrite it. If the file does not exist, create a new file.
A Open a file for append. If the file already exists, the file pointer is placed at the end of the file. That is to say, the new content will be written to the existing content. If the file does not exist, create a new file for writing.
AB Open a file in binary format for append. If the file already exists, the file pointer is placed at the end of the file. That is to say, the new content will be written to the existing content. If the file does not exist, create a new file for writing.
A + Open a file for reading and writing. If the file already exists, the file pointer is placed at the end of the file. When the file is opened, the append mode is used. If the file does not exist, create a new file for reading and writing.
AB + Open a file in binary format for append. If the file already exists, the file pointer is placed at the end of the file. If the file does not exist, create a new file for reading and writing.

 

File object

File is an object, which has some built-in attributes, as shown below:

      

File object attributes Description
File. closed Indicates that the file has been closed; otherwise, the value is False.
File. mode Access Mode used to open an Access File
File. encoding Encoding used by the file
File. name File Name
File. newlines When the row delimiter is not read, it is None, and only one row separator is a string. When the file has multiple types of row Terminator, it is a list containing all the currently encountered rows.
File. softspace If the value is 0, a space character is added after the output, and 1 indicates no. This attribute is generally not required by programmers and is used internally by the program.

 

  

Read () read (size) readline () readlines ()

In the previous example, the read () function is used to read all the content of a file at a time. If you can ensure the file size, you can naturally. However, if the file is too large, the memory will pop up. Therefore, you can call the read (size) method repeatedly to read a maximum of size bytes each time. You can also call readline () each time you read a row of content, you can call readlines () to read all the content at a time and return the list by row. All in all, based on your needs. Take a txt file as an example. Reading other files requires special processing. In addition, you need to pay attention to the format encoding method of the files. Here, we only introduce the reading method. Other topics will be discussed here.

Create poet.txt under D: \ python_file. The example is as follows. Because a Chinese character occupies multiple bytes, the read (size) part is garbled, for example:

    

    

 

    

 

Write ()

Writing a file is the same as reading a file. The only difference is that when the open () function is called, the input identifier 'W' or 'wb 'indicates writing a text file or writing a binary file; the append parameter corresponding to 'A.

As shown in the following example, because the write.txt file does not exist, create the file and write it to it:

    

On the basis of the preceding example, the file is overwritten:

    

Continue. This append operation will append at the end of the file:

    

 

 

 

 

 

 

    

 

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.