Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is the seek() Function?
3.
Syntax of the seek() Function
4.
Parameters
5.
How the seek() Function Works
6.
Example 1: Using seek() in Read Mode
6.1.
Python
7.
Example 2: Using seek() in Write Mode
7.1.
Python
8.
Example 3: Using seek() to Move the File Pointer
8.1.
Python
9.
Frequently Asked Questions
9.1.
What happens if you seek() beyond the end of the file? 
9.2.
Can you use seek() in binary mode?
9.3.
What are the alternatives to seek() for random file access?
10.
Conclusion
Last Updated: Jul 26, 2024
Easy

Seek Function in Python

Author Rinki Deka
0 upvote

Introduction

File handling is a fundamental aspect of programming, and Python provides powerful tools to work with files. One of these tools is the seek() function, which allows you to change the file's current position. This function is crucial when you need to read or write data from specific locations in a file. 

Seek Function in Python

In this article, we'll explore the seek() function in Python, understand its syntax, see how it works with examples, and cover best practices and Frequently Asked Questions.

What is the seek() Function?

The seek() function in Python is used to change the position of the file's read/write pointer within the file. This is particularly useful when you need to move to a specific location in a file to read from or write to that position.

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

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

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

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 AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass