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
What does the list.remove(x) method do in Python ?
- Removes the element at index x from the list
- Removes the first occurrence of value x from the list
- Removes all occurrences of value x from the list
- Removes the last occurrence of value x from the list
If a table which has one Primary key and two alternate keys. How many Candidate keys will this table have ?
- 1
- 2
- 3
- 4
State whether the following statement is True or False:
The
finally
block in Python is executed only if no exception occurs in the try block.What will be the output of the following code ?
c = 10 def add(): global c c = c + 2 print(c, end = '#') add() c = 15 print(c, end = '%')
- 12%15#
- 15#12%
- 12#15%
- 12%15#