Move around within the file using seek(). : Seek : File PYTHON TUTORIALS


PYTHON TUTORIALS » File » Seek »

 

Move around within the file using seek().


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



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo File
» Seek