Before we get into the seek() and tell() functions, let's first understand what a file pointer is.
As we know, when you use the open() function to open a file and read its contents, you will always start reading from the first character (byte) of the file. So, is there any way to customize the starting position for reading? The answer is yes, which requires moving the file pointer position.
The file pointer is used to indicate the starting position of reading and writing to the file. If you think of a file as a stream of water, each piece of data in the file (opened in b-mode, each piece of data is a byte; opened in normal mode, each piece of data is a character) is equivalent to a drop of water, and the file pointer indicates where in the file the file will be read from. Figure 1 briefly illustrates the concept of a file pointer.
Figure 1 Schematic diagram of the file pointer concept
As you can see, by moving the file pointer and using the read() and write() functions, you can easily read data from (or write data to) the specified location 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 write position will not move backward on its own, and the newly written data will directly overwrite the data at that position in the file.
To move the file pointer, the file object provides tell() and seek() functions. tell() is used to determine the current location of the file pointer, while seek() is used to move the file pointer to a specified location in the file.
tell() function
The tell() function is simple to use and has the following basic syntax:
()
where file represents the file object.
For example, in the same directory, write the following program to read a file with the following contents:
The code for reading is as follows:
f =open("",'r') print(()) print((3)) print(())
The results of the run are:
0
htt
3
As you can see, when you use the open() function to open a file, the file pointer starts at 0, which means it's at the beginning of the file, and when you use the read() function to read three characters from the file, the file pointer moves back three characters at the same time. This means that when the program uses the file object to read or write data, the file pointer will automatically move backward: as many data are read or written, the file pointer will automatically move backward by as many positions.
seek() function
The seek() function is used to move the file pointer to a specified location, the syntax of the function is formatted as follows:
(offset[, whence])
Where the meaning of each parameter is as follows:
- file: represents a file object;
- whence: as an optional parameter, it is used to specify the location where the file pointer is to be placed, there are three choices for this parameter: 0 for the header (default), 1 for the current location, and 2 for the end of the file.
- offset: the offset of the file pointer relative to the whence position, a positive number indicates a backward offset, a negative number indicates a forward offset. For example, when whence == 0 &&offset == 3 (i.e., seek(3,0)), the file pointer is moved to a position 3 characters away from the beginning of the file; when whence == 1 &&offset == 5 (i.e., seek(5,1)), the file pointer is moved backward to a position 5 characters away from the current position. 5 characters from the current position.
Note that when offset is non-zero, Python requires that the file be opened in binary format or an error will be thrown.
The following program demonstrates file pointer manipulation:
f =open('','rb') # Determine the location of the file pointer print(()) # Read a byte, the file pointer is automatically moved back 1 data print((1)) print(()) # Move the file pointer 5 characters back from the beginning of the file. (5) print(()) print((1)) # Move the file pointer back 5 characters from the current position. (5,1) print(()) print((1)) # Move the file pointer from the end of the file, forward to a position 2 characters away (-1,2) print(()) print((1))
The results of the run are:
0
b'h'
1
5
b'/'
11
b'a'
21
b't'
————————————————
to this article on the specific use of Python seek() and tell() function is introduced to this article, more related Python seek() tell() content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!