One: Open file
Open (Name,mode,[buffersize])
Name: File path
Mode: File open mode
Two: File read
read()Method: The entire contents of the file can be read at once, and Python reads the contents into memory, represented by an str object.
ReadLine ([size]): reads one line at a time by default if no parameters are passed. If size< a line length, the size byte is read. If size> a line length, only one row is read.
ReadLines (): one read IO. Default_buffer_size rows, approximately 586 rows, return the read result as an array of strings.
Iterative reads: You can use the ITER (file) function to convert an open file into an iterative object, and then have for...in iterated access, reading one line at a time.
File=open ("filename.txt",'r+') iter_f=iter ( File) for in iter_f: using str
Three: File Write
Write (str): Writes STR to the file. Note: A string must be written. If you write something else, convert it to a string and write again.
Writelines (SOME_STR): A parameter can be a string tuple, or a string object that can be iterated
Write Cache and write disk:
After calling write (str)/writelines (SOME_STR), it simply writes the content to memory, and it is not updated to the file at this time.
An explicit F.flush () or f.close () is required to force the cache data in memory to be brushed into the disk before data can be written to disk.
When the data in the write cache is larger than the buffer capacity, the Python interpreter automatically flushes the buffer to write the data to disk with a buffer size of 155648 bytes .
Four: file pointer and random read and write
In Python, the data cannot be read immediately after it has been written, it must be turned off before it can be opened, and the read content cannot be read again. This is because the file pointer moves: When the file opens, the file pointer points to the beginning of the file, at which point the Read/write action moves the file pointer and reads and writes. Once read and write is complete, the pointer stays at the end of the operation. The next time the call is read and written, the file pointer continues to be moved to read and write, where it was last stopped.
Random Read and write implementation: After understanding the movement principle of the file pointer, we can control the movement of the pointer, to read and write the file we want to manipulate the location.
File pointer move instruction: File.seek (offset, start position)
There are three types of starting points: OS. Seek_set (file start), OS. Seek_end (end of file), OS. Seek_cur (file pointer current position)
Offset: Can be a positive number, or it can be negative. Positive numbers move down, negative numbers move forward. If the moving range exceeds the file range, an error occurs.
Five: File close
F.close ()
The reason for the file to close after the operation:
1: Forcing data written to the file to be brushed into disk
2: The number of files in the system that allow each process to open simultaneously is limited
Python Learning Note six: File processing