A, b and the difference between t Mode Mode
1.1, control file to read and write content model
1.1.1, t mode
(1) are read as a string (Unicode) units
(2) only for reading and writing text
(3) must be specified character coding, i.e. encoding parameters must be specified
1.1.2, b parameters
(1) are read in binary (bytes) as a unit
(2) can be for any type of file
(3) must not be specified character coding, i.e. encoding parameter can not be specified
(4) manually codec
1.1.3 summary
(1) When a plain text file operation, using the t mode may help us omit coding and decoding process, b mode it is necessary to manually encoding and decoding, so that this time t is more convenient mode.
(2) the time for non-text files (pictures, videos, etc.), you can only use the b mode operation,
Read and write files in a disposable 1.2, b mode
1.2.1, read the file rb
Open with (R & lt ' d.txt ' , MODE = ' RB ' ) AS F: RES = reached, f.read () # Binary utf-8 of Print (RES, type (RES)) # read binary code Print (RES. decode ( ' UTF-. 8 ' )) # reads out the contents of the file
1.2.2, write to the file wb
Open with (R & lt ' f.txt ' , MODE = ' WB ' ) AS F: f.write ( ' Hello Hello ' .encode ( ' UTF-. 8 ' )) # must be added encode f.write ( ' ha ' .encode ( ' gbk ' )) # read only mode or in gbk write utf-8
1.2.3, the file copy tool rb + wb
= INPUT in SRC_FILE of ( ' source file path >>: ' ) .strip () and dst_file = INPUT ( ' source file path >>: ' ) .strip () with Open (R & lt ' {} ' .format (in SRC_FILE of), MODE = ' RB ' ) AS F1, \ Open (R & lt ' {} ' .format (and dst_file), MODE = ' WB ' ) AS F2: RES = f1.read () # presence of too large memory footprint may f2.write (res ) for Line in f1: # optimization methods f2.write (line)
1.3, loop reads the file, read and write points
1.3.1, in bytes, control their own amount of data each time data is read
with open(r'test.jpg',mode='rb') as f: while True: res=f.read(1024) # 1024 if len(res) == 0: break print(len(res))
1.3.2 to read and write per unit of behavior, but there may be a one-time single-line read data too large
1.3.2.1, the cycle time for pattern file w
with open(r'g.txt',mode='rt',encoding='utf-8') as f: for line in f: print(len(line),line)
1.3.2.2, the cycle time for the file mode b
Open with (r ' g.txt ' , the MODE = ' rb ' ) AS f: for Line in f: Print (Line) # contents of the file are letters or numbers, or need to add encode
1.3.2.3, b mode for the next cycle of reading pictures
Open with (R & lt ' test.jpg ' , MODE = ' RB ' ) AS F: for Line in F: Print (Line) # contents of this form of numbers and letters, there is no increase encode
Second, other methods of file operations
2.1, read related operations
2.1.1, while files w cycle time mode, readline (read only one line)
with open(r'g.txt',mode='rt',encoding='utf-8') as f: res1=f.readline() res2=f.readline() print(res2) #使用while循环读取 while True: line=f.readline() if len(line) == 0: break print(line)
2.1.2, the b mode, multi-line one-time write. As list form. readlines
with open(r'g.txt',mode='rt',encoding='utf-8') as f: res=f.readlines() print(res)
2.1.3, the difference between the read and readlines
read is read directly by file content, readlines the contents of the file is read out as a list
Note: read the contents of the file and disposable readlines are read into memory, if the content is too large, the memory overflow might be used for single-line read cycle, or read (n) limits the number of read bytes disposable.
2.2, write-related operations
2.2.1, disposable multi-line write writelines
f.writelines (): with Open ( ' h.txt ' , MODE = ' wt ' , encoding = ' UTF-. 8 ' ) AS F: # use write write f.write ( ' 1111 \ N222 \ n3333 \ n- ' ) # use + write cycle for writing # L = [ '11111 \ n-', '2222', '3333', 4444] L = [ ' 11111 \ n- ' , ' 2222 ' , ' 3333 ' ] for Line in L : f.the Write (Line) # use writelines write f.writelines(l)
Write 2.2.2, under b mode file
Open with ( ' h.txt ' , MODE = ' WB ' ) AS F: L = [ ' 1111aaa1 \ n- ' .encode ( ' UTF-. 8 ' ), ' 222bb2 ' .encode ( ' UTF-. 8 ' ), ' 33eee33 ' .encode ( ' UTF-. 8 ' ) ] # supplement 1: if it is pure English characters can be directly obtained prefix bytes type b L = [ b ' 1111aaa1 \ n- ' , b'222bb2 ' , B ' 33eee33 ' ] # Supplement 2:' on '.encode (' utf-8 ' ) identical to bytes (' on 'encoding =' UTF-. 8 ') L = [ bytes ( ' on ah ' , = encoding ' UTF-. 8 ' ), bytes ( ' punch die ' , = encoding ' UTF-. 8 ' ), bytes ( ' small trash were ' , encoding = ' UTF-. 8 ' ), ] f.writelines (L)
2.3, write hard disk immediately flush
with open('h.txt', mode='wt',encoding='utf-8') as f: f.write('哈') # f.flush()
2.4, to understand
Open with ( ' ../../../../Desktop/Python lecture notes / day12 notes /h.txt ' , MODE = ' wt ' , encoding = ' UTF-. 8 ' ) AS F: Print (F whether .readable ()) whether a file readable print if (f.writable ()) whether a file can be written print (f.encoding) format, such as 'UTF-. 8 ,' if the file attribute is open b, then the attribute is not print (f.name) output absolute path print if (f.closed) determine the file is closed
Three pointer movement, the control file
3.1, the pointer is moved in units bytes / bytes, but the read (n) at t mode, n-represents the number of characters.
Open with ( ' aaa.txt ' , MODE = ' RT ' , encoding = ' UTF-. 8 ' ) AS F: RES = reached, f.read (. 4 ) Print (RES) # output of the first four bytes
3.2, pointer movement f.seek (a, b)
3.2.1, mode 0, i.e., b = 0, a reference file headers
f.seek(9,0) f.seek(3,0) # 3
with open('aaa.txt',mode='rb') as f:
f.seek(9,0)
f.seek(3,0) # 3
# print(f.tell())
f.seek(4,0)
res=f.read()
print(res.decode('utf-8'))
3.2.2, mode 1, i.e., b = 1, the current pointer position is a reference
f.seek(9,1) f.seek(3,1) # 12
with open('aaa.txt',mode='rb') as f:
f.seek(9,1)
f.seek(3,1) # 12
print(f.tell())
3.2.3, mode 2, i.e., b = 2, the end of the reference file is
f.seek(-9,2) # 3 f.seek(-3,2) # 9
with open('aaa.txt',mode='rb') as f:
f.seek(-9,2)
# print(f.tell())
f.seek(-3,2)
# print(f.tell())
print(f.read().decode('utf-8'))
Note: Mode 0 t can be used in mode 0 and 1 and 2 may be used in the mode b
3.2.4, the current pointer position f.tell ()
f.tell () # get the current position of the file pointer