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
- Open the first file by using open(“readcontent.txt”).
- Read the lines using readlines().
- Print the position of the cursor using file.tell()
- Move the cursor to the 10th position using file.seek(10).
- 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