Syntax of the seek() Function
The basic syntax of the seek() function is:
file_object.seek(offset, whence)
Parameters
- offset: This is the number of bytes to move the file pointer. It can be positive or negative.
- whence: This is an optional argument that specifies the reference point for the offset. It can take one of the following values:
- 0 (default): The file's beginning.
- 1: The current file position.
- 2: The file's end.
How the seek() Function Works
The seek() function changes the file pointer position based on the offset and the reference point (whence). If whence is set to 0, the offset is calculated from the beginning of the file. If whence is 1, the offset is added to the current file position. If whence is 2, the offset is added to the end of the file.
Example 1: Using seek() in Read Mode
Let's start with a simple example where we use seek() in read mode.
Python
with open("sample.txt", "r") as file:
file.seek(5) # Move the file pointer to the 6th byte
content = file.read(10) # Read the next 10 bytes
print(content)
Explanation:
- The file is opened in read mode.
- The seek(5) moves the file pointer to the 6th byte from the beginning.
- The read(10) reads the next 10 bytes from that position.
Output
ple text
Example 2: Using seek() in Write Mode
Next, let's see how to use seek() in write mode.
Python
with open("sample.txt", "r+") as file:
file.seek(10) # Move the file pointer to the 11th byte
file.write("INSERTED") # Write new content at that position
Explanation:
- The file is opened in read and write mode (r+).
- The seek(10) moves the file pointer to the 11th byte.
- The write("INSERTED") writes the string "INSERTED" starting at that position.
Output
After running the code, the file content changes accordingly.
Example 3: Using seek() to Move the File Pointer
In this example, we'll move the file pointer around using different values for whence.
Python
with open("sample.txt", "rb") as file:
file.seek(-5, 2) # Move the pointer to 5 bytes before the end
content = file.read() # Read from that position to the end
print(content)
Explanation:
- The file is opened in binary read mode (rb).
- The seek(-5, 2) moves the file pointer to 5 bytes before the end of the file.
- The read() reads from that position to the end of the file.
Output
b'xt file.'
Frequently Asked Questions
What happens if you seek() beyond the end of the file?
If you seek beyond the end of the file in read mode, the subsequent read operations will return an empty string. In write mode, it may create a gap filled with null bytes.
Can you use seek() in binary mode?
Yes, seek() can be used in binary mode (rb, wb, ab). The file pointer moves in bytes.
What are the alternatives to seek() for random file access?
For large files or databases, you might use memory-mapped files or specialized libraries that offer more efficient random access methods.
Conclusion
The seek() function is a powerful tool in Python for managing file pointer positions. It enables precise control over reading and writing operations in a file. By understanding and using the seek() function effectively, you can enhance your file handling capabilities in Python.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.