Python seek() function

Python seek() function

The concept of file handling is used to preserve the data or information generated after running the program. Like other programming languages like C, C++, Java, Python also support file handling.

seek() method

In Python, seek() function is used to change the position of the File Handle to a given specific position. File handle is like a cursor, which defines from where the data has to be read or written in the file.

Syntax:, from_what), where f is file pointer
Offset: Number of positions to move forward
from_what: It defines point of reference.
Returns: Does not return any value

The reference point is selected by the from_what argument. It accepts three values:

  • 0: sets the reference point at the beginning of the file
  • 1: sets the reference point at the current file position
  • 2: sets the reference point at the end of the file

By default from_what argument is set to 0.
Note: Reference point at current position / end of file cannot be set in text mode except when offset is equal to 0.
Example 1: Let’s suppose we have to read a file named “GfG.txt” which contains the following text:

"Code is like humor. When you have to explain it, it’s bad."
# Python program to demonstrate
# seek() method
# Opening "GfG.txt" text file
f = open("GfG.txt", "r")
# Second parameter is by default 0
# sets Reference point to twentieth
# index position from the beginning
# prints current position


When you have to explain it, it’s bad.


Example 2: Seek() function with negative offset only works when file is opened in binary mode. Let’s suppose the binary file contains the following text.

b'Code is like humor. When you have to explain it, its bad.'


# Python code to demonstrate
# use of seek() function
# Opening "GfG.txt" text file
# in binary mode
f = open("data.txt", "rb")
# sets Reference point to tenth
# position to the left from end, 2)
# prints current position
# Converting binary to string and
# printing


, its bad.

Last Updated on November 13, 2021 by admin

Leave a Reply

Your email address will not be published. Required fields are marked *

Recommended Blogs