Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python:How to change the position in the file from current file position? [duplicate]

I want to change the file position from the current file position to another position.Suppose my current file position is 13 and I want to change this file position to the 18. I use the seek() method as follow but it shows some error.

Code:-

fileobj = open("intro.txt","r");
content = fileobj.read(13);
pos = fileobj.tell();
print("Current position : ",pos);
fileobj.seek(5,1); #Change position from current position to next five character.

Error

fileobj.seek(5,1);

io.UnsupportedOperation: can't do nonzero cur-relative seeks

I use python 3.4.3.How can I do this?

like image 961
Aditya Avatar asked Aug 13 '15 04:08

Aditya


1 Answers

Your code works in Python 2, but not in 3. You must open the file as binary:

fileobj = open("intro.txt","rb");
like image 131
cdarke Avatar answered Nov 14 '22 22:11

cdarke