Python File Seek
This post will present a detailed guide on the “file.seek()” method by covering the following supported content:
- Python “file.seek()” Method.
- Using “file.seek()” Method to Change/Modify the Position of the File Handle.
- Using “file.seek()” Method With Negative Offset.
Python “file.seek()” Method
The “file.seek()” method is utilized to read or write data at a particular position in a file. This method moves the file handle, which is like a cursor that marks where the data will be accessed, to a different location in the file.
Syntax
Parameter
In the above syntax, the “file” is the file pointer, “offset” is the position to move forward, and “from_where” is the point of reference. The “from_where” argument can take three values:
- “0”: implies that the file’s reference point is at the beginning.
- “1”: implies that the file’s reference point is at the current file position.
- “2”: implies that the file’s reference point is at the file end.
The “from_where” argument is assigned as “0” by default.
Return Value
The “seek()” method retrieves a value representing the cursor’s new position in the file.
File to be Evaluated
We have a file named “filename.txt” that contains the following text and will be evaluated throughout the guide:
Example 1: Using the “file.seek()” Method to Change the Position of the File Handle
Here is an example code to change the position of the file handle:
print(file.seek(20))
print(file.readline())
file.close()
According to the above code:
- The “seek()” method takes the integer value as an argument and moves the specified number of positions forward by referring to the provided file.
- The “readline()” method is utilized to read the data from the defined position until the end of the specified file.
Output
The position of the file handle and the data from the current position until the end has been displayed in the above snippet.
Example 2: Using the “file.seek()” Method With Negative Offset
The below code is used to change the file handle position of the file opened in binary mode utilizing the “file.seek()” method:
print(file.seek(-24, 2))
print(file.readline())
file.close()
In the above code:
- The “open()” function opened the file “txt” in “rb” mode.
- The “seek()” method takes the negative offset and the reference point value as its arguments, respectively and moves the file handle from position “-24” relative to the end of the file.
- The “readline()” method is then utilized to read the rest of the line.
Output
Based on the above output, the specified data from the text file is read from the specified position.
Conclusion
The “file.seek()” method in Python is utilized to move or modify the position of the file handle pointer according to the particular offset value. This method can read a certain section/portion of a file or write at a particular portion of the file. The negative offset along with the end reference point value can be utilized to move the pointer to the specified location starting from the end of the file. This blog presented a detailed guide on Python’s “seek()” method using the relevant examples.
Source: linuxhint.com