Python file operation
The difference between read() method, readline() method and readlines() method
1.read([size]) method
Read size bytes from the current position of the file. If there is no parameter size, it means that it is read until the end of the file. Its scope is a string object
Print 3 bytes
Output result

read() method
Print all content by default

Output result

2.readline() method
It can be seen from the literal meaning that this method reads one line of content at a time, so it occupies less memory when reading and is more suitable for large files. This method returns a string object.

Output result:

Because readline reads by line and prints by line, and the print function is output by default and needs to cross lines! So there is a blank line in the middle of every printed line
3.readlines() method
Read all lines of the entire file and save them in a list variable, each line as an element, but reading large files will take up more memory

Output result:

Add a string after a certain line (but this method costs memory)

Output result:

Does not occupy memory:

1. How to open and read text file content 2. Use the open function to open the file and return an IO object, which has 3 methods for reading the file: read, readline and readlines. Please use code to ...
This article mainly records the difference between the three functions read(), readline() and readlines() for manipulating files in python. Conclusion: .read() reads the entire file every time, it usu...
The three common methods for python to read files are read(), read(), readline() and readlines(). The differences between the three are as follows. 1. Remove the contents of a.txt file 2. read([size])...
First, read () method The read ([size]) method reads the SIZE byte from the current location. If there is no parameter size, the read to the end of the file, return a string object Output results: Sec...
The printed result is: In other words, readlines() splits the contents of the file by newline characters, all in the list. The printed result is: Readline() reads the contents of the file by newline, ...
Organize the methods of read, readline, and readlines in Python3. For the contents of the file open mode, please refer to my previous article. The contents of the file runoob.txt are as follows: Read ...
An example shows the difference between the three Reprinted at: https://blog.51cto.com/alwaysyunwei/1364398...
1、read() is read to read all the contents of the text, the type of the return value is str 2、readline() readline is a single line to read the file, return type is str 3、readlines() readlines also a re...
.read() Each reading of the entire file, which is usually in the end will read the contents of a file into a string variable, i.e. .read () generates a file content type is a string, as shown bel...
Public number added a new section, is a day for everyone to answer a Python common interview questions, but do not bite off more than a day, one day at a title, just right, just hope this interview co...