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.

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

edit
close

play_arrow

link
brightness_4
code

# 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 postion
print(f.tell())
  
print(f.readline()) 
f.close()

chevron_right


Output:

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

edit
close

play_arrow

link
brightness_4
code

# 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()

chevron_right


Output:

47
, its bad.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.