5

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?

Aditya
  • 1,214
  • 7
  • 19
  • 29

1 Answers1

6

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

fileobj = open("intro.txt","rb");
cdarke
  • 42,728
  • 8
  • 80
  • 84