Python offers several methods for file handling. In addition to the standard operations like reading and writing to the files, there are methods to manipulate the file pointer effectively.
In this tutorial, you’ll learn how to use the seek() function to move the position of a file pointer while reading or writing a file.
Table of contents
Goals of this lesson:
- Learn to use the
seek()method to move the file cursor ahead or backward from the current position - Learn to move the file pointer to that start or end of the file
- Learn to move the file pointer backward from the end of the file
- Get the current position of the file handle
What is seek() in Python
The seek() function sets the position of a file pointer and the tell() function returns the current position of a file pointer.
A file handle or pointer denotes the position from which the file contents will be read or written. File handle is also called as file pointer or cursor.
For example, when you open a file in write mode, the file pointer is placed at the 0th position, i.e., at the start of the file. However, it changes (increments) its position as you started writing content into it.
Or, when you read a file line by line, the file pointer moves one line at a time.
Sometimes we may have to read only a specific portion of the file, in such cases use the seek() method to move the file pointer to that position.
For example, use the seek() function to do the file operations like: –
- Read a file from the 10th character.
- Directly jump to the 5th character from the end of the file.
- Add new content to file after a particular position.
How to Use seek() Method
To change the file handle’s position use seek() method. As we discussed, the seek() method sets the file’s current position, and then we can read or write to the file from that position.
Syntax:
f.seek(offset, whence)Code language: Python (python)
How many points the pointer will move is computed from adding offset to a reference point; the reference point is given by the whence argument.
The allowed values for the whence argument are: –
- A
whencevalue of 0 means from the beginning of the file. - A
whencevalue of 1 uses the current file position - A
whencevalue of 2 uses the end of the file as the reference point.
The default value for the whence is the beginning of the file, which is 0
Refer to the below table for clear understanding.
| Seek Operation | Meaning |
|---|---|
f.seek(0) | Move file pointer to the beginning of a File |
f.seek(5) | Move file pointer five characters ahead from the beginning of a file. |
f.seek(0, 2) | Move file pointer to the end of a File |
f.seek(5, 1) | Move file pointer five characters ahead from the current position. |
f.seek(-5, 1) | Move file pointer five characters behind from the current position. |
f.seek(-5, 2) | Move file pointer in the reverse direction. Move it to the 5th character from the end of the file |
Example
Consider the following example where we are reading a text file contents with the offset as 6. It means we will start reading the file directly from the 6th character.