Seek() python – Python File seek() Method with Examples

Files in Python:

Seek() python: Python file handling is a way of saving program output to a file or reading data from a file. File handling is a crucial concept in the programming world. File management is used in almost every form of project. Assume you are constructing an inventory management system. You have data in the inventory management system related to sales and purchases, thus you must save that data somewhere. Using Python’s file management, you can save that data to a file. You must be given data in the form of a comma-separated file or a Microsoft Excel file if you wish to conduct data analysis. Using file handling, you can read data from a file and write output back into it.

File seek() Method in Python:

File seek python: The seek() function is a built-in Python method that is used to set the current file position in a file stream.

The seek() method returns the new position as well.

Syntax:

file.seek(offset)

Parameters

offset: Required. A number denoting the position at which the current file stream position should be set.

Return Value: This method’s return type is <class ‘int’>, and it returns the new file position.

File seek() Method with Examples in Python

Example1

Approach:

  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
  • Print the content of the given file using the read() function.
  • Apply seek() method to the given file by passing some random number as an argument.
  • It returns the file content from that index position.
  • Print the content of the given file using the read() function after applying seek() method.
  • Close the given file using the close() function.
  • The Exit of the Program.

Below is the implementation:

# Make a single variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Open the file in read-only mode. In this case, we're reading the contents of the file.
gvn_file = open(givenFilename, 'r') 
# Print the content of the given file using the read() function
print("The given file content:")
print(gvn_file.read())
print()
# Apply seek() method to the given file by passing some random number as an argument.
# It returns the file content from that index position.
gvn_file.seek(7)
# Print  the content of the given file using the read() function after applying seek() method
print("The given file content after applying seek method:")
print(gvn_file.read())
# Close the given file using the close() function
gvn_file.close()

Output:

The given file content:
Hello this is btechgeeks
Good morning btechgeeks 
welcome to python-coding platform

The given file content after applying seek method:
his is btechgeeks
Good morning btechgeeks 
welcome to python-coding platform

Example2: To Return the new position

Approach:

  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
  • Print the content of the given file using the read() function.
  • Apply seek() method to the given file by passing some random number as an argument to it and print it. It returns the new position.
  • Close the given file using the close() function.
  • The Exit of the Program.

Below is the implementation:

# Make a single variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Open the file in read-only mode. In this case, we're reading the contents of the file.
gvn_file = open(givenFilename, 'r') 
# Print the content of the given file using the read() function
print("The given file content:")
print(gvn_file.read())
print()
# Apply seek() method to the given file by passing some random number as an argument to it
# and print it. It returns the new position 
print(gvn_file.seek(7))
# Close the given file using the close() function
gvn_file.close()

Output:

The given file content:
Hello this is btechgeeks
Good morning btechgeeks 
welcome to python-coding platform

7

File Content:

Hello this is btechgeeks
Good morning btechgeeks 
welcome to python-coding platform

Google Colab Images:

Files and Code: