File Operating Modes
Read the file
document content
Moonlight, suspected ground frost
I raise my eyes to the moon, looking down and think of home
1.read()
Read all the contents of the file
f = open('libai',encoding = 'utf-8') print(f.read()) Moonlight, suspected ground frost I raise my eyes to the moon, looking down and think of home
2.readline()
Only reads a single line
f = open('libai',encoding = 'utf-8') print(f.readline()) Moonlight, suspected ground frost
3.readlines()
The content of the article with a newline character segmentation, and produces a list format (large volumes of data when not recommended)
f = open('libai',encoding = 'utf-8') print(f.readlines()) [ ' Moonlight, suspected ground frost \ n- ' , ' give the first moon, down and think of home ' ]
4. seek and tell the cursor
f = open('libai',encoding='utf-8') Data = reached, f.read () # default cursor at the starting position, read () after reading, the cursor to the end of the file data2 reached, f.read = () # contents read data2 is empty Print (Data) Print ( data2) f.close () # close the file
file
abcdefg
hyjklmn
opqrst
vwxyz
Open = F ( ' libai ' , encoding = ' UTF-. 8 ' ) # Tell gets the current cursor Print (f.tell ()) # 0 print(f.readline().strip()) print(f.readline().strip()) print(f.tell()) f.seek (0) # Move the cursor to the file where the starting Print (f.readline (). Strip ()) result: 0 abcdefg hyjklmn 18 abcdefg
5.flush Refresh
Simulation progress bar
import sys,time for i in range(40): sys.stdout.write('#') sys.stdout.flush () # the flush cache memory pleasant force data is written to disk time.sleep (0.1)
6.with statement
with code block after the implementation, the file is automatically turned off
with open('libai') as f: print(f.read())
7. binary reading "rb"
# RB reading the binary mode F = Open ( ' libai ' , ' RB ' ) # mainly used for network transmission Print (f.readline ())
b'\xe5\xba\x8a\xe5\x89\x8d\xe6\x98\x8e\xe6\x9c\x88\xe5\x85\x89\xef\xbc\x8c\xe7\x96\x91\xe6\x98\xaf\xe5\x9c\xb0\xe4\xb8\x8a\xe9\x9c\x9c\r\n'
8. binary writing "wb"
f = open('libai','wb') f.write('中文'.encode())
Transcoding
method
File Operating Modes
Read the file
document content
Moonlight, suspected ground frost
I raise my eyes to the moon, looking down and think of home
1.read()
Read all the contents of the file
f = open('libai',encoding = 'utf-8') print(f.read()) Moonlight, suspected ground frost I raise my eyes to the moon, looking down and think of home
2.readline()
Only reads a single line
f = open('libai',encoding = 'utf-8') print(f.readline()) Moonlight, suspected ground frost
3.readlines()
The content of the article with a newline character segmentation, and produces a list format (large volumes of data when not recommended)
f = open('libai',encoding = 'utf-8') print(f.readlines()) [ ' Moonlight, suspected ground frost \ n- ' , ' give the first moon, down and think of home ' ]
4. seek and tell the cursor
f = open('libai',encoding='utf-8') Data = reached, f.read () # default cursor at the starting position, read () after reading, the cursor to the end of the file data2 reached, f.read = () # contents read data2 is empty Print (Data) Print ( data2) f.close () # close the file
file
abcdefg
hyjklmn
opqrst
vwxyz
Open = F ( ' libai ' , encoding = ' UTF-. 8 ' ) # Tell gets the current cursor Print (f.tell ()) # 0 print(f.readline().strip()) print(f.readline().strip()) print(f.tell()) f.seek (0) # Move the cursor to the file where the starting Print (f.readline (). Strip ()) result: 0 abcdefg hyjklmn 18 abcdefg
5.flush Refresh
Simulation progress bar
import sys,time for i in range(40): sys.stdout.write('#') sys.stdout.flush () # the flush cache memory pleasant force data is written to disk time.sleep (0.1)
6.with statement
with code block after the implementation, the file is automatically turned off
with open('libai') as f: print(f.read())
7. binary reading "rb"
# RB reading the binary mode F = Open ( ' libai ' , ' RB ' ) # mainly used for network transmission Print (f.readline ())
b'\xe5\xba\x8a\xe5\x89\x8d\xe6\x98\x8e\xe6\x9c\x88\xe5\x85\x89\xef\xbc\x8c\xe7\x96\x91\xe6\x98\xaf\xe5\x9c\xb0\xe4\xb8\x8a\xe9\x9c\x9c\r\n'
8. binary writing "wb"
f = open('libai','wb') f.write('中文'.encode())
Transcoding
method