Python file tell – Python Read File from Given Index (Example of tell() and seek()) Methods

Files in Python:

Python file tell: 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.

In this article, we will learn about two key python file handling methods: tell() and seek(). We will learn how to set the file pointer position at a specified position and read data from there.

File tell() method in Python:

Seek in python: The tell() method returns the current position of the file pointer.

File seek() method in Python:

The seek() method sets the position of the file pointer at a specific position.

Syntax:

file.seek(offset, whence)

Parameters

offset: The offset is the position of the file’s read/write pointer.
whence: whence is an optional parameter with three possible values.

  • 0 – seeks the file pointer from the current position
  • 1 – to seek the file pointer from the file’s starting position
  • 2 – seek the file pointer from the file’s end position

Read File from Given Index (Example of tell() and seek()) Methods in Python

Example

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 write mode. In this case, we’re writing the contents into the file.
  • Write some random text into the given file using the write() function.
  • Close the given file using the close() function.
  • Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
  • Print the name of the file using the name property.
  • Pass some random number to the read() method to read the file content up to that index position(excludes the last index).
  • Print the above result file content.
  • Get the current position of the file pointer using the tell() method.
  • Print the current position of the file pointer.
  • Pass some random number to the read() method to read the file content up to that index(next 8) position(excludes the last index).
  • Print the above result file content.
  • Get the current position of the file pointer using the tell() method.
  • Print the current position of the file pointer.
  • Send the pointer back again to the top using the seek() function.
  • Print the current position of the file pointer.
  • Pass some random number to the read() method to read the file content up to that index position(excludes the last index).
  • Print the above result file content.
  • Get the current position of the file pointer using the tell() method.
  • Print the current position of the file pointer.
  • 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 write mode. In this case, we're writing the contents into the file.
gvn_file = open(givenFilename, 'w')
# Write some random text into the given file using the write() function
gvn_file.write("good morning this is btechgeeks")
# Close the given file using the close() function
gvn_file.close()

# Open the file in read-only mode. In this case, we're simply reading the contents of the file.
gvn_file = open(givenFilename, 'r') 
# Print the name of the file using the name property
print("The given file's name is: ",gvn_file.name)
# Pass some random number to the read() method to read the file content 
# upto that index position(excludes last index)
rslt_file = gvn_file.read(8)
# Print the above result file content
print("The given file content upto 8th index = ", rslt_file)
# Get the current position of the file pointer using the tell() method
currnt_positn= gvn_file.tell()
# Print the current position of the file pointer 
print("The Current Position of the file pointer = ", currnt_positn)

# Pass some random number to the read() method to read the file content 
# upto that index position(excludes last index)
rslt_file = gvn_file.read(8)
# Print the above result file content
print("The given file content upto next 8th index = ", rslt_file)
# Get the current position of the file pointer using the tell() method
currnt_positn= gvn_file.tell()
# Print the current position of the file pointer 
print("The Current Position of the file pointer = ", currnt_positn)
print()

# Send the pointer back again to the top using the seek() function
print("Send the pointer back again to the top")
currnt_positn = gvn_file.seek(0,0)
# Print the current position of the file pointer 
print("The Current Position of the file pointer = ", currnt_positn)
# Pass some random number to the read() method to read the file content 
# upto that index position(exculdes last index)
rslt_file=gvn_file.read(18)
print("The given file content upto next 18th index = ",rslt_file)
# Get the current position of the file pointer using the tell() method
currnt_positn= gvn_file.tell()
# Print the current position of the file pointer 
print("The Current Position of the file pointer = ", currnt_positn)
# Close the given file using the close() function
gvn_file.close()

Output:

The given file's name is: samplefile.txt
The given file content upto 8th index = good mor
The Current Position of the file pointer = 8
The given file content upto next 8th index = ning thi
The Current Position of the file pointer = 16

Send the pointer back again to the top
The Current Position of the file pointer = 0
The given file content upto next 18th index = good morning this 
The Current Position of the file pointer = 18

File Content:

good morning this is btechgeeks

Google Colab Images:

Files and Code:

Leave a Comment