Last Updated : 12 Jul, 2025
In Python, the seek() function is used to move the file cursor to a specific position inside a file. This allows you to read or write at any part of the file instead of always starting from the beginning. For example, if you want to skip the first 10 characters while reading a file, you can do:
Python
f = open("demo.txt", "r")
f.seek(10)
print(f.read())
f.close()
This will move the file cursor to position 10 and begin reading from there.
file.seek(offset, from_what)
Parameters:
Returns:
Note: In text mode, from_what can only be 0. Using 1 or 2 requires binary mode ('rb').
Examples Example 1: Using seek() in Text ModeLet's say GfG.txt contains:
PythonCode is like humor. When you have to explain it, it’s bad.
f = open("GfG.txt", "r")
f.seek(20)
print(f.tell())
print(f.readline())
f.close()
Output:
20
When you have to explain it, it’s bad.
Explanation:
Suppose data.txt contains the following binary content:
Pythonb'Code is like humor. When you have to explain it, its bad.'
f = open("data.txt", "rb")
f.seek(-10, 2)
print(f.tell())
print(f.readline().decode('utf-8'))
f.close()
Output:
47
, its bad.
Explanation:
Refer the below article to understand the basics of File Handling.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4