Program to move the cursor to a specific position in a file

In this python file program, we will move the cursor to a specific position in a file with the help of the below-given steps. Let’s consider we have readcontent.txt file with the below content.

				
					#readcontent.txt
Line1 : This is India.
Line2 : This is America.
Line3 : This is Canada.
Line4 : This is Australia.

				
			
Steps to solve the program
  1. Open the first file by using open(“readcontent.txt”).
  2. Read the lines using readlines().
  3. Print the position of the cursor using file.tell()
  4. Move the cursor to the 10th position using file.seek(10).
  5. Again print the position of the cursor using file.tell().
				
					# Open the file
file = open('readcontent.txt')
# Read lines in the file
file.readline()
# Print position of the curosr
print("Position of a cursor in the file: ",file.tell())
# Move cursor to a specific Position
file.seek(10)
# Again print position of the cursor
print("Position of a cursor in the file: ",file.tell())
				
			

Output :

Position of a cursor in the file: 23 
Position of a cursor in the file: 10


 

find the cursor position in a file.

read the content of the file in reverse order.

Leave a Comment