Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Seek Function in Python

File Handling is considered a fundamental concept in programming. Python provides several features for this purpose of File Handling. seek() function is one such function. In this article, we will discuss the seek function in python in detail. We will see how to use the seek function to set the position of the file pointer within a file with the help of examples.

What is Seek() Function in Python?

The seek function is a built-in function in Python that is used to set the current position of the file pointer within a file. The file pointer is a marker that indicates the current position in the file, and it is used to read or write data from that point. This function is particularly useful when reading or writing large files, as it allows us to move the file pointer to a specific location within the file and read or write data from that point.

Syntax of Seek Function in Python

The syntax of the seek function in Python is as follows:

file.seek(offset, whence) 

Here, file is the file object that we want to set the position of the file pointer in.

Parameters of Seek Function in Python

The first argument, offset, is the number of bytes we want to move the file pointer. The second argument, whence, specifies the reference position from where we want to move the file pointer. The possible values of whence are

  • 0 (default): refers to the beginning of the file
  • 1: refers to the current position of the file pointer
  • 2: refers to the end of the file

Example of Seek Function in Python

Let’s see an example of how to use the seek function to set the position of the file pointer within a file:

data.txt File:

PrepBytes is an Ed-Tech Company.

Code:

# Open a file in read mode
file = open("data.txt", "r")

# Set the position of the file pointer to byte 10
file.seek(10)

# Read the next 5 bytes from the file
data = file.read(5)

# Print the data that was read
print(data)

# Close the file
file.close()

Output:

is an

Explanation:
In this example, we opened a file called data.txt in read mode and set the position of the file pointer to byte 10 using the seek function. We then read the next 5 bytes from the file using the read function and printed the data that was read which gives us an output as “is an”. Finally, we closed the file using the close function.

Different Modes of the Seek Function in Python

The seek function can be used in three different modes: absolute mode, relative mode, and from end mode. Let’s see each of these modes in detail:

Absolute Mode

In absolute mode, the offset parameter specifies the number of bytes from the start of the file where we want to set the file pointer. In other words, the file pointer is set to an absolute position within the file.

The syntax for using the seek function in python in absolute mode is as follows:

file.seek(offset, 0)

Here, the second parameter is 0, which specifies that we want to set the file pointer in absolute mode.

Let’s see an example of how to use the seek function in absolute mode:

data.txt File:

PrepBytes is an Ed-Tech Company.

Code:

# Open a file in read mode
file = open("data.txt", "r")

# Set the position of the file pointer to byte 10
file.seek(10, 0)

# Read the next 5 bytes from the file
data = file.read(5)

# Print the data that was read
print(data)

# Close the file
file.close()

Output:

is an

Relative Mode

In relative mode, the offset parameter specifies the number of bytes to move the file pointer from its current position. In other words, the file pointer is set to a position relative to its current position.

The syntax for using the seek function in relative mode is as follows:

file.seek(offset, 1) 

Here, the second parameter is 1, which specifies that we want to set the file pointer in relative mode.

Let’s see an example of how to use the seek function in relative mode:

data.txt File:

PrepBytes is an Ed-Tech Company.

Code:

# Open a file in read mode
file = open("data.txt", "r")

# Set the position of the file pointer to 10 bytes from the current position
file.seek(10, 1)

# Read the next 5 bytes from the file
data = file.read(5)

# Print the data that was read
print(data)

# Close the file
file.close()

Output:

is an

Explanation:
In this example, we opened a file called data.txt in read mode and set the position of the file pointer to 10 bytes from its current position using the seek function in relative mode. We then read the next 5 bytes from the file using the read function and printed the data that was read. Finally, we closed the file using the close function.

From End Mode

In from end mode, the offset parameter specifies the number of bytes from the end of the file where we want to set the file pointer. In other words, the file pointer is set to a position relative to the end of the file.

The syntax for using the seek function in from end mode is as follows:

file.seek(offset, 2)

Here, the second parameter is 2, which specifies that we want to set the file pointer in from end mode.

Let’s see an example of how to use the seek function in from end mode:

data.txt File:

PrepBytes is an Ed-Tech Company.

Code:

# Open a file in read mode
file = open("data.txt", "r")

# Set the position of the file pointer to 10 bytes from the end of the file
file.seek(-10, 2)

# Read the next 5 bytes from the file
data = file.read(5)

# Print the data that was read
print(data)

# Close the file
file.close()

Output:

h Com

Explanation:
In this example, we opened a file called data.txt in read mode and set the position of the file pointer to 10 bytes from the end of the file using the seek function in from end mode. We then read the next 5 bytes from the file using the read function and printed the data that was read. Finally, we closed the file using the close function.

Using the Tell Function

The tell function is another built-in function in Python that is used to get the current position of the file pointer within a file.

The syntax of the tell function is as follows:

file.tell() 

Here, file is the file object for which we want to get the current position of the file pointer.

Let’s see an example of how to use the tell function:

data.txt File:

PrepBytes is an Ed-Tech Company.

Code:

# Open a file in read mode
file = open("data.txt", "r")

# Get the current position of the file pointer
position = file.tell()

# Print the current position of the file pointer
print(position)

# Close the file
file.close()

Output:

0

Explanation:
In this example, we opened a file called data.txt in read mode and used the tell function to get the current position of the file pointer. We then printed the current position of the file pointer which shows “0” on the console and closed the file using the close function.

Conclusion
The seek function is a very useful built-in function in Python that allows us to set the position of the file pointer within a file. We can use it in absolute mode, relative mode, or from end mode to set the position of the file pointer based on the beginning, current, or end of the file, respectively. In addition, the tell function allows us to determine the current position of the file pointer within a file. By understanding how to use the seek function, we can manipulate files in more advanced ways and create more powerful programs.

Seek Function in Python – FAQs

Ques 1. What happens if we set the file pointer to a position beyond the end of the file?
Ans. If we set the file pointer to a position beyond the end of the file, any subsequent read or write operations will return an empty string or write to a new location.

Ques 2. Can we use the seek function to set the position of the file pointer in write mode?
Ans. Yes, we can use the seek function to set the position of the file pointer in write mode as well. However, we must be careful when doing this because any data that was previously in the file beyond the new position of the file pointer will be lost.

Ques 3. How can we determine the current position of the file pointer within a file?
Ans. We can determine the current position of the file pointer within a file using the tell function.

Ques 4. Can we use the seek function to set the position of the file pointer in binary mode?
Ans. Yes, we can use the seek function to set the position of the file pointer in binary mode as well.

Ques 5. What is the default mode for the seek function?
Ans. The default mode for the seek function is the absolute mode.

Leave a Reply

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