Python File seek() Method – Tutorial with Examples

Python File seek() Method

The seek() method in Python is a built-in method used to change the current position of the file pointer. It moves the file pointer to a specific position in the file, which can be specified by an offset from a certain position. The file pointer is used to determine where the next read or write operation will occur.

Syntax

file.seek(offset, whence)

offset – The number of bytes to move the file pointer by. If the offset is positive, the file pointer is moved forward; if the offset is negative, the file pointer is moved backward.

whence (optional) – The position to which the offset is relative. It can take one of the following values:

  • 0 – The offset is relative to the beginning of the file (default)
  • 1 – The offset is relative to the current position of the file pointer
  • 2 – The offset is relative to the end of the file

Return Value

The seek() method does not return any value.

Examples

Example 1: Move the file pointer to the beginning of the file

In this example, we will use the seek() method to move the file pointer to the beginning of the file:

file = open("example.txt", "r")
file.seek(0)
print(file.read())
file.close()

In this example, we opened a file named “example.txt” in read mode, moved the file pointer to the beginning of the file using the seek() method with an offset of 0 and printed the contents of the file to the console.

Output:

This is the first line of the file.
This is the second line of the file.
This is the third line of the file.

Example 2: Move the file pointer to a specific position in the file

In this example, we will use the seek() method to move the file pointer to a specific position in the file:

file = open("example.txt", "r")
file.seek(15)
print(file.read())
file.close()

In this example, we opened a file named “example.txt” in read mode, moved the file pointer to a position 15 bytes from the beginning of the file using the seek() method with an offset of 15 and printed the contents of the file from that position to the end of the file to the console.

Output:

econd line of the file.
This is the third line of the file.

Example 3: Move the file pointer relative to the end of the file

In this example, we will use the seek() method to move the file pointer relative to the end of the file:

file = open("example.txt", "r")
file.seek(-10, 2)
print(file.read())
file.close()

In this example, we opened a file named “example.txt” in read mode, moved the file pointer to a position 10 bytes from the end of the file using the seek() method with an offset of -10 and a whence value of 2 (which means the offset is relative to the end of the file) and printed the contents of the file from that position to the end of the file to the console.

Output:

e third line of the file.

Use Cases

The seek() method is useful in situations where you need to read or write data from a specific position in a file. For example, if you are reading a large file and need to skip over a certain number of bytes before processing the data, you can use the seek() method to move the file pointer to the appropriate position. Similarly, if you need to append data to the end of a file, you can use the seek() method to move the file pointer to the end of the file before writing the new data.

Conclusion

The seek() method in Python is a powerful tool for working with files. It allows you to easily move the file pointer to a specific position in a file, making it possible to read or write data from that position. By understanding how to use the seek() method, you can more effectively work with files in your Python programs.

Leave a Reply

Your email address will not be published. Required fields are marked *