Python:如何从当前文件位置更改文件中的位置?

时间:2015-08-13 04:02:55

标签: python python-3.x file-handling

我想将文件位置从当前文件位置更改为另一个位置。假设当前文件位置为13,我想将此文件位置更改为18。 我使用seek()方法如下,但它显示了一些错误。

代码: -

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.

错误

fileobj.seek(5,1);

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

我使用python 3.4.3。我该怎么做?

1 个答案:

答案 0 :(得分:5)

您的代码适用于Python 2,但不适用于3.您必须将文件打开为二进制文件:

fileobj = open("intro.txt","rb");
相关问题