Python file operations

Source: Internet
Author: User

1. When talking about Python file operations, be sure to first say the function open () of the operation file in Python and say about the arguments in the open () function according to the two Open methods encoding

With open (' La.txt ', ' R ', encoding= ' utf-8 ') as F:

Pass

With open (' la.txt ', ' RB ') as F:

Pass

First, when opened according to "R" mode, you can increase the encoding (encoding) parameter, do not increase the default encoding= ' Utf-8 ', and in "RB" mode is opened without the need to encode (encoding) parameters, this is why, People think about the file on the hard disk is stored in bytes as binary, and in the "R" mode when opened, you need to read the string, and from the hard disk to get a byte, so Python needs to convert the bytes into a string, the process needs to encode, you need to encode bytes in what encoding into a string, While this process is open in this mode, Python does so by default. When opened in "RB" mode, you need to read bytes, and the hard drive is the byte itself, so no coding is required.

Okay, here's the r+ w+ A + three open mode:

    • r+ mode when opening a file:

1. When opening a file, the file pointer is at the beginning of the 0 position, F.tell () = 0

2. If read (1) specifies a specific parameter in read, where 1 means reading a character, after reading, the pointer jumps to the specified position and continues reading from that position if read continues

3. If there is write () in the read process, the pointer will jump to the last, and the appended way to write

    • w+ Mode Open File:

1.w+ means to write and read

2. When opening a file in w+ mode, clear the contents of the file, the file pointer is 0-bit

3.write () After the file pointer jumps to the last

4. If you want to read the content you need to adjust the pointer position F.seek = 0

    • A + mode open file

1.a+ means to write and read

2. After opening the file in w+ mode, the file pointer jumps to the last

3.write (), the content is appended

4. If you want to read the content you need to adjust the pointer position F.seek = 0

Finally, for F.seek,f.tell values are bytes

Python file operations

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.