Python Open Detailed

Source: Internet
Author: User

The mode of opening a file is:

1, R, read-only mode "default".

2, W, write-only mode. "Unreadable, does not exist, is created, the content is deleted"

3, A, append mode. "Readable, non-existent, created, only append content exists"

Second, + means you can read and write a file at the same time

1, R +, can read and write files. "Readable, writable, can be added"

2, w+, write read

3, A + with a

Three, "U" means that when reading the data, \ r \ n \ r \ nthe automatic conversion to \ n (in conjunction with R or r+ mode)

RU

R+u

Four, "B" means processing binary files (such as: FTP send upload ISO image file, Linux can be ignored, Windows processing binary needs to be labeled)

Rb

Wb

Ab

V. Read by character

#read Specify read characters
f = open (' Test.log ', ' R ', encoding= ' utf-8 ')
ret = F.read (2) #按照2个字符读, Python2 is read in 2 bytes.
F.close ()
Print (ret)

VI. Tell
#tell pointer at a byte
f = open (' Test.log ', ' R ', encoding= ' utf-8 ')
Print (F.tell ()) #查看当前指针位置
F.read (2)
Print (F.tell ())
ret = F.read (2) #按照2个字符读, Python2 is read in 2 bytes.
F.close ()

Seven, Seek
#seek
f = open (' Test.log ', ' R ', encoding= ' utf-8 ')
F.seek (1) #指定当前指针位置.
F.read ()
F.close ()
Print (ret)
Eight, F.truncate

File Test.log starts with: ABCDEFG

f = open (' Test.log ', ' r+ ', encoding= ' utf-8 ')
F.seek (3)
F.truncate () #截取光标前面的内容并保存到原文件
F.close ()
The contents of this file change to: ABC

Python Open Detailed

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.