Python seek() Method
The seek()
method in Python is used with file objects to change the current position of the file cursor. This is particularly useful when working with file handling operations, allowing you to move the cursor to a specific position in a file before reading or writing.
Syntax of seek()
file.seek(offset, whence)
Parameters
offset
(required) – The number of bytes to move the cursor.whence
(optional, default:0
) – Specifies the reference position foroffset
. It can take the following values:0
(default) → Move the cursor relative to the beginning of the file.1
→ Move the cursor relative to the current position (requires the file to be opened in binary mode).2
→ Move the cursor relative to the end of the file (requires binary mode).
How seek()
Works?
The seek()
method moves the file pointer (cursor) based on the offset
and whence
values:
- If
whence = 0
,offset
is counted from the beginning of the file. - If
whence = 1
,offset
is counted from the current cursor position. - If
whence = 2
,offset
is counted from the end of the file.
Example 1: Moving Cursor to a Specific Position
# Create a sample file
with open("example.txt", "w") as file:
file.write("Hello, this is a test file.")
# Open file in read mode
with open("example.txt", "r") as file:
file.seek(7) # Move cursor to the 7th byte
print(file.read()) # Read from position 7
Output:
this is a test file.
The cursor moves to the 7th byte (index 7), and the file is read from there.
Example 2: Using seek()
with whence=1
(Relative to Current Position)
with open("example.txt", "rb") as file: # Binary mode required for whence=1
file.seek(10) # Move to 10th byte
print(file.read(5)) # Read 5 bytes
file.seek(5, 1) # Move cursor 5 bytes forward from current position
print(file.read(5)) # Read next 5 bytes
Output:
is a
test
The first seek(10)
moves the cursor to byte 10, reading is a
. Then, seek(5, 1)
moves 5 bytes forward and reads test
.
Example 3: Using seek()
with whence=2
(Relative to End of File)
with open("example.txt", "rb") as file:
file.seek(-10, 2) # Move 10 bytes before the end of the file
print(file.read()) # Read the last 10 bytes
Output:
test file.
The cursor moves 10 bytes before the end and reads from there.
Example 4: Using seek()
with Write Mode
with open("example.txt", "r+") as file: # Read & write mode
file.seek(7) # Move cursor to position 7
file.write("Python") # Overwrite content from position 7
with open("example.txt", "r") as file:
print(file.read()) # Read the modified file
Output:
Hello, Python is a test file.
The word “this” at position 7 gets replaced with “Python”.
Example 5: Checking Cursor Position with tell()
with open("example.txt", "r") as file:
file.seek(10)
print(file.tell()) # Get current cursor position
Output:
10
The cursor is at position 10.
Key Points
1. Binary Mode (b
flag) Required for whence=1
and whence=2
)
whence=1
(relative seek) andwhence=2
(end of file seek) only work in binary mode (rb
,wb
, etc.).- In text mode (
r
,w
),seek()
only supportswhence=0
.
2. Overwriting Behavior
- When using
seek()
in write mode (w
orr+
), writing new data overwrites existing content instead of inserting new data.
3. Handling Large Files
seek()
is useful for handling large files efficiently, as you can jump directly to required parts instead of reading the entire file.
Conclusion
seek(offset, whence)
moves the cursor in a file.whence=0
moves from the beginning,whence=1
moves from the current position, andwhence=2
moves from the end.- Use
tell()
to check the cursor position. seek()
works in both read and write modes but behaves differently.