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