Python file read/write

Source: Internet
Author: User

Mode Description
R Read-only
r+ Write
W Write, delete the original file before re-writing, if the file is not created
w+ Read and write, first delete the original file, re-write, if the file is not created (can write output)
Mode Description
A Writes, appends new content to the end of the file, and the file does not exist, creates
A + Read-write, append new content to the end of file, file does not exist, create
B Open the binary file. Can be used in conjunction with r,w,a,+
U All line break symbols are supported. "\ r", "\ n", "\ r \ n"

File Object methods:

string=fileobject.readline ()#只读取一行内容

list=fileobject.readlines ()#读取文件里面所有的内容, put the contents of each line in the file into a list

string=fileobject.read ()#读取全部内容

Fileobject.write (String)

Fileobject.writelines (list)

Fileobject.close ()

Fileobject.next ()

Fileobject.seek (offset, option (0 first, 1 current position, 2 tail))

Filebject.flush () #更新文件内容

Filebject.tell ()#查看当前文件指针的位置

Filebject. truncate ()#清空文件内容

Python file read/write

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.