Before explaining the seek function and tell function, let's first understand what a file pointer is.
We know that when you use the open function to open a file and read the contents of the file, it always starts with the first character (byte) of the file. So, is there a way to specify the starting position for reading? The answer is yes, this requires moving the position of the file pointer.
The file pointer is used to indicate the starting position of the file reading and writing. If the file is viewed as a stream, each data in the file (opened in b mode, each data is a byte; opened in normal mode, each data is a character) is equivalent to a water drop, and the file pointer is marked “This is where the file will start to read”.
It can be seen that by moving the position of the file pointer and then using the read and write functions, it can be easily implemented to read the data at the specified position in the file (or write data to the specified position in the file).
Note that when writing data to a file, if it is not the end of the file, the original data at the writing position will not move backwards by itself, and the newly written data will directly overwrite the data at that position in the file.
To implement the movement of the file pointer, the file object provides a tell function and a seek function. The tell function is used to determine the current position of the file pointer, and the seek function is used to move the file pointer to the specified position of the file.
The use of the tell function is simple, and its basic syntax is as follows:
Where file represents a file object.
For example, in the same directory, write the following program to read the a.txt file. The content in the a.txt file is:
The code to read a.txt is as follows:
f = open("a.txt",'r') print(f.tell()) print(f.read(2)) print(f.tell())
The output is:
It can be seen that when the file is opened using the open function, the starting position of the file pointer is 0, which means that it is located at the beginning of the file. After reading 2 characters from the file using the read function, the file pointer moves to the same time. And moved by 2 characters. This shows that when the program uses the file object to read and write data, the file pointer will automatically move backwards: how many data is read and written, the file pointer will automatically move backwards by as many positions.
The seek function is used to move the file pointer to the specified position. The syntax of this function is as follows:
file.seek (offset [, whence])
The meaning of each parameter is as follows:
Note that when the offset value is non-zero, Python requires that the file must be opened in binary format, otherwise an io.UnsupportedOperation error will be thrown.
The following program demonstrates file pointer operations:
f = open ('a.txt', 'rb') # Determine the position of the file pointer print (f.tell()) # Read a byte, the file pointer automatically moves back by 1 data print (f.read(1)) print (f.tell()) # Move the file pointer from the beginning of the file to the 5 character position f.seek(5) print (f.tell()) print (f.read(1)) # Move the file pointer from the current position to the 5 character position f.seek (5, 1) print (f.tell()) print (f.read(1)) # Move file pointer from end of file to 10 characters f.seek (-1, 2) print (f.tell()) print (f.read(1))
The output is:
Note: Because seek is used in the program, a non-zero offset is used, so the file must be opened with b, otherwise an io.UnsupportedOperation error will be reported, and interested readers can try it for themselves.
The above program demonstrates using the seek method to move the file pointer, including counting from the beginning of the file, the current position of the pointer, and the end of the file. Run the above program and combine the output of the program to experience the effect of the file pointer movement.
More Tutorials:Python Installation - Linux (Ubuntu)
More Python Exercises:Python String Exercises