- 1

What does seek() do ??

7th Jul 2020, 5:09 PM
AYUSH KUMAR SRIVASTAVA
2 Answers
+ 6
seek() is a method of the file object. seek allows to set the position of the file pointer inside the file. If you are reading lines from a file, the file pointer is moving to the next position = next line. if all lines are consumed, file.read() does not return anything, beause the file pointer is at the EOF position. To start reading again, you can use seek(0), which sets the pointer to the beginning of the file. Then reading can be start again. For reading, the file pointer has not necessarily to be at the begin of a line, but can have a value so that reading starts at that defind position. fp = open('txt.txt', 'r') fp.seek(2) # reading starts at 3rd byte print(fp.readline()) # file content is: 2.562021e-4 '3.2' # output is: 562021e-4 '3.2'
7th Jul 2020, 6:49 PM
Lothar
Lothar - avatar