Move around within the file using seek(). : seek « File « Python Tutorial






f = open('/tmp/x', 'w+')
print f.tell()
f.write('test line 1\n')         # add 12-char string [0-11]
print f.tell()
print f.write('test line 2\n')   # add 12-char string [12-23]
print f.tell()                   # tell us current file location (end))
print f.seek(-12, 1)             # move back 12 bytes
print f.tell()                   # to beginning of line 2
print f.readline()
print f.seek(0, 0)               # move back to beginning
print f.readline()
print f.tell()                   # back to line 2 again
print f.readline()
print f.tell()                   # at the end again
print f.close()                  # close file








12.14.seek
12.14.1.Random accesss file
12.14.2.Move around within the file using seek().