File operations in Python

Source: Internet
Author: User

I. Initial knowledge of document operations

Using Python to read and write files is a very simple operation. We use the open () function to get to a file handle by opening a file. Then you can do all sorts of things with a file handle. There are also differences in the actions you can perform depending on how you open them.

How to open the file: R, W, A, r+, w+, A +, RB, WB, AB, R+b, w+b, a+b the default is R (read-only) mode

Two. Read only, write only, append

2.1 Read-only--r

As the name implies, can only read, oper (file name, pattern, encoding format), such as Oper (' 1.TXT ', mode = ' r ', encoding = ' utf-8 '), filename is a must, mode default is read-only r,encoding default is Utf-8, But will be affected by the operating system, so in the win platform this parameter I was manually set to Utf-8

    

f = open ("1.txt", mode="R", encoding="utf-8  "= f.read ()print(content) F.close ()

After opening, remember to close with close. F is the handle that is used to manipulate the file, where the read file has read (), ReadLine (), ReadLines () Three methods, the first one is to read out the entire file, the second one is read one line at a time, and the third is to read all the But put each row in a list, return the list, and finally use the For loop handle to return one line at a time, such as for lines in F:

2.2 Write-only--w

W mode overwrites the original content, which requires special attention

2.3 Additional--a

A mode will append new content at the end of the file without worrying about overwriting the problem.

Triple, plus (+) mode

3.1 Read and write--r+

Read-write mode, can be read or write, but must be read first, because the cursor is the default at the beginning of the file, ready to read. If you want to write first, it will overwrite the original content. In this mode, if the content is read, no matter where the cursor is at that time, the file is written or manipulated at the end.

3.2 Write-read--w+

First empty the original content and then write, and finally read, but the read is empty, this mode is meaningless. Someone would say that it would be better to read it first? NoNoNo, this mode, the beginning is not read content, slag.

3.3 Additional Read--a+

In this mode, read-ahead and post-read are not read content, but also slag, useless

Four. B Mode

In fact, in various modes, the characters are replaced by bytes.

Five. Other related operations

1. Seek (n)

The cursor moves to the n position, and the unit of movement is byte, so if it's utf-8 Chinese, remember if it's a multiple of 3.

Usually we use this function to move to the beginning or the end.

Seek (0): Move to the beginning

Seek (0,2): Moves to the end, the second parameter represents the offset from, the default 0 for the beginning, 1 for the current position, and 2 for the end

2.tell ()

Using tell () we can get the current position of the cursor, I use this function to practice the time there is a suspected buf problem, interested can move links: https://bbs.pythontab.com/thread-35039-1-1.html, Look forward to a big can answer the next

3.truncate (N)

This function is used to truncate a file, from the beginning to the current position, n to intercept n characters from the beginning

File operations in Python

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.