Basic Python-based file operations 01

Source: Internet
Author: User

One, the file operation


When you open a file, you need to specify the file path and how you want to open the file, and then open it to get the file handle and manipulate it later through the file handle.


The mode of opening the file is:


R: Read-only: default mode, pointer at start

W: Write only "write only; overwrite if present, create"

A: Append "Append: Exists" Append, does not exist create in write "


r+: Read and write: The pointer defaults to the beginning, and overwrites the target location content when writing

w+: Write read "write read: Overwrite exists, do not exist then create"

A +: Write "write read: There is append, there is no creation in writing"


RB or R+b "opens 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. 】

WB or W+b "opens a file in binary format for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file. 】

AB or A+b "opens a file in binary format for appending. If the file already exists, the file pointer will be put "


Rb+ "opens a file in binary format for read-write. The file pointer will be placed at the beginning of the file. 】

Wb+ "opens a file in binary format for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file. 】

Ab+ "opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file to read and write. 】


Open with no B is a string type

Open with B is byte (binary) type


Open () #打开文件

F.seek () #调整指针位置

F.write () #写入内容

F.close () #关闭文件

F.tell () #获取指针的位置

F.read () #读取文件内容, read from the post to


F.flush () #刷新缓冲区

F.readline () #读一行

F.truncate () #截取内容, to intercept the contents of the current pointer, directly manipulate the original file


With open ("1.txt.py", "r+",) as F: Do not write F.close ()

Support Open two files simultaneously, multiple separated by commas

Application scenario to copy 1.txt content to 2.txt,

With open ("1.txt", "R") as File1, open ("2.txt", "W") as File2:

For line in File1:

File2.write (line)

Read three cards for each line of a file:

# method 1

f = open ("2.txt", "r+")

line = F.readline ()

While line:

Print Line

line = F.readline ()

F.close ()

# method 2

f = open ("2.txt", "r+")

ret = F.readlines ()

For line in RET:

Print Line

F.close ()

# Method 3

f = open ("2.txt", "r+")

For line in F.readlines ():

Print Line

F.close ()


This article is from the "Lifelong Learning" blog, please be sure to keep this source http://20120809.blog.51cto.com/10893237/1980328

Basic Python-based file operations 01

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.