This article speaks to read the file in Python and uses the difference between the read (), readline () and readlines () method of the three.
read: read the entire file.
readline: read the next line, the method using generators.
readlines: read the entire file into an iterator for us to traverse.
Here's a look at the implementation code.
Test1.txt to prepare a document, which reads as follows:
<ignore_js_op>
Read () method reads the file from the current position until the end of the file, as a String object that range:
<ignore_js_op>
result output is:
<ignore_js_op>
the readline () method of each line is read content, therefore, read the small memory footprint, more suitable for large files, the method returns a string object:
<ignore_js_op>
output:
<ignore_js_op>
the readlines () to read the entire file all lines stored in a list (list) variable per line as an element, but read large files would be more total memory:
<ignore_js_op>
output is:
<ignore_js_op>
more technical information may concern: gzitcast
Difference read, readline, readlines of
Guess you like
Origin www.cnblogs.com/heimaguangzhou/p/11696424.html
Recommended
Ranking