1. read(): Read all the content of the file at one time, suitable for small files. Examples are as follows:
with open('123.txt', 'r') as f:
print(f.read(), end='') # end=''用来关闭print默认添加换行符
2. read(size): Read at most size bytes each time, suitable for large files. Examples are as follows:
with open('123.txt', 'r') as f:
while True:
str = f.read(16) # 每次读取16字节
if not str:
break
print(str, end='')
3. readline(): Read one line at a time. Examples are as follows:
with open('123.txt', 'r') as f:
while True:
str = f.readline() # 每次读取一行
if not str:
break
print(str, end='')
4. readlines(): Read all the content at once and return a list by line, suitable for configuration files. Examples are as follows:
with open('123.txt', 'r') as f:
for line in f.readlines():
print(line, end='')