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

Adi*_*tya 5 python file-handling python-3.x

我想将文件位置从当前文件位置更改为另一个位置。假设我当前的文件位置为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.
Run Code Online (Sandbox Code Playgroud)

错误

fileobj.seek(5,1);

io.UnsupportedOperation: can't do nonzero cur-relative seeks
Run Code Online (Sandbox Code Playgroud)

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

cda*_*rke 6

您的代码在Python 2中有效,但在3中无效。您必须以二进制文件形式打开文件:

fileobj = open("intro.txt","rb");
Run Code Online (Sandbox Code Playgroud)