Python:How 从当前文件位置更改文件中的位置?
Python:How to change the position in the file from current file position?
我想将文件位置从当前文件位置更改为另一个文件位置position.Suppose我当前文件位置是 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.How 我可以这样做吗?
您的代码在 Python 2 中有效,但在 3 中无效。您必须以二进制形式打开文件:
fileobj = open("intro.txt","rb");
我想将文件位置从当前文件位置更改为另一个文件位置position.Suppose我当前文件位置是 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.How 我可以这样做吗?
您的代码在 Python 2 中有效,但在 3 中无效。您必须以二进制形式打开文件:
fileobj = open("intro.txt","rb");