Python seek() function - GeeksforGeeks (2024)

Improve

Improve

Like Article

Like

Save

Report

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.

Refer the below article to understand the basics of 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: f.seek(offset, from_what), where f is file pointer
Parameters:
Offset: Number of positions to move forward
from_what: It defines point of reference.
Returns: Return the new absolute position.

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." 

Python3

# 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

f.seek(20)

# prints current position

print(f.tell())

print(f.readline())

f.close()

Output:

20When 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.'

Python3

# 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

f.seek(-10, 2)

# prints current position

print(f.tell())

# Converting binary to string and

# printing

print(f.readline().decode('utf-8'))

f.close()

Output:

47, its bad.


Last Updated : 28 Apr, 2022

Like Article

Save Article

Share your thoughts in the comments

Please Login to comment...

Python seek() function - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Barbera Armstrong

Last Updated:

Views: 6402

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.