KnowledgeBoat Logo

Computer Science

Write the missing statement to complete the following code:

file = open("example.txt", "r")
data = file.read(100)
............... #Move the file pointer to the beginning of the file
next_data = file.read(50)
file.close()

Python File Handling

1 Like

Answer

file.seek(0)

Reason — To move the file pointer to the beginning of the file, we can use the seek() method. When we pass 0 as the offset to this function, it moves the file pointer to the beginning of the file.

So the complete code is:

file = open("example.txt", "r")
data = file.read(100)
file.seek(0)  # Move the file pointer to the beginning of the file
next_data = file.read(50)
file.close()

Answered By

2 Likes


Related Questions