ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python -- seek定位文件指针位置 错误 io.UnsupportedOperation: can't do nonzero cur-relative seeks错误

2019-05-04 12:40:15  阅读:398  来源: 互联网

标签:UnsupportedOperation cur 错误 open 偏移 print position seek tell


f=open("E:/test/悯农.txt",'r')
str=f.read(17)
print("读取的数据是:",str)
position=f.tell()
print("当前位置:",position)


f.seek(4,0)  #从头开始,偏移4个字节
position=f.tell()
print("当前位置:",position)

 
f.seek(4,1)  #从当前位置开始,偏移4个字节
position=f.tell()
print("当前位置:",position)

f.seek(-4,2)
position=f.tell()
print("当前位置:",position)


f.close()

 

 

 

总结:

seek中whence参数的值:

0:open函数以r,w,带b的二进制模式,就是以任何模式打开文件,都能正常运行

1和2:open函数只能以二进制模式打开文件,才能正常运行,否则就会报出上面的错误

 

Python的官方文档的解释:

链接地址:https://docs.python.org/3/tutorial/inputoutput.html?highlight=seek

>In text files (those opened without a b in the mode string), only seeks relative to the beginning of the file are allowed (the exception being seeking to the very file end with seek(0, 2)) and the only valid offset values are those returned from the f.tell(), or zero. Any other offset value produces undefined behaviour.

翻译:

    在文本文件中(那些在模式字符串中没有b打开的文件),只允许相对于文件的开头进行查找(例外情况是使用seek(0,2)查找文件的结尾),并且唯一有效的偏移值是从f.tell()或零返回的偏移值。任何其他偏移值都会产生未定义的行为。

标签:UnsupportedOperation,cur,错误,open,偏移,print,position,seek,tell
来源: https://www.cnblogs.com/bravesunforever/p/10808126.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有