python3 in , There are three ways to read files :read()、readline()、readlines().
These three methods , Both support receiving a variable , Used to limit the amount of data read each time , however , It's not usually used .
Purpose of this paper : analysis 、 Summarize the usage and characteristics of the above three reading methods .
characteristic : Read entire file , Put the contents of the file in a string variable .
shortcoming : If the file is very large , Especially when it is larger than memory , Can't use read() Method .
file = open(' Contact information of department colleagues .txt', 'r') # Create this file , Is an iterative object
try:
text = file.read() # The result is str type
print(type(text)) # Print text The type of
print(text)
finally:
file.close() # Close file file
"""
<class 'str'>
Li Fei 177 70 13888888
Wang Chao 170 50 13988888
Baijing 167 48 13324434
huangshan 166 46 13828382
"""
read() Read the bytes directly into the string , Including line breaks
>>> file = open(' Part time model contact information .txt', 'r')
>>> a = file.read()
>>> a
' Li Fei 177 70 13888888\n Wang Chao 170 50 13988888\n Baijing 167 48 13324434\n huangshan 166 46 13828382'
characteristic :readline() Method reads one row at a time ; It returns a string object , Keep the memory of the current row
shortcoming : Than readlines It's much slower
file = open(' Contact information of department colleagues .txt', 'r')
try:
while True:
text_line = file.readline()
if text_line:
print(type(text_line), text_line)
else:
break
finally:
file.close()
"""
<class 'str'> Li Fei 177 70 13888888
<class 'str'> Wang Chao 170 50 13988888
<class 'str'> Baijing 167 48 13324434
<class 'str'> huangshan 166 46 13828382
"""
readline() Read entire line , Include line terminators , And return... As a string
>>> file = open(' Part time model contact information .txt', 'r')
>>> a = file.readline()
>>> a
' Li Fei 177 70 13888888\n'
characteristic : Read the entire file at once ; Automatically parse the contents of the file into a list of lines
'''
No one answers the problems encountered in learning ? Xiaobian created a Python Learning exchange group :711312441
Looking for small partners who share the same aspiration , Help each other , There are also good video tutorials and PDF e-book !
'''
file = open(' Contact information of department colleagues .txt', 'r')
try:
text_lines = file.readlines()
print(type(text_lines), text_lines)
for line in text_lines:
print(type(line), line)
finally:
file.close()
"""
<class 'list'> [' Li Fei 177 70 13888888\n', ' Wang Chao 170 50 13988888\n', ' Baijing 167 48 13324434\n', ' huangshan 166 46 13828382']
<class 'str'> Li Fei 177 70 13888888
<class 'str'> Wang Chao 170 50 13988888
<class 'str'> Baijing 167 48 13324434
<class 'str'> huangshan 166 46 13828382
"""
readlines() Read all lines , Then return them as a list of strings .
>>> file = open(' Part time model contact information .txt', 'r')
>>> a = file.readlines()
>>> a
[' Li Fei 177 70 13888888\n', ' Wang Chao 170 50 13988888\n', ' Baijing 167 48 13324434\n',
' huangshan 166 46 13828382']