随机存取

文件操作中,read()和write()都属于顺序存取,只能按顺序从头到尾的进行读写。实际上我们可以只访问文件中我们感兴趣的部分。对此,我们可以使用seek()和tell()方法。两种方法的help()返回结果如下。

 >>> f = open("demo.txt","w")
>>> f.write("")
10
>>> f.close()
 >>> help(f.seek)
Help on built-in function seek: seek(cookie, whence=0, /) method of _io.TextIOWrapper instance
Change stream position. Change the stream position to the given byte offset. The offset is
interpreted relative to the position indicated by whence. Values
for whence are: * 0 -- start of stream (the default); offset should be zero or positive
* 1 -- current stream position; offset may be negative
* 2 -- end of stream; offset is usually negative Return the new absolute position.
 >>> help(f.tell)
Help on built-in function tell: tell() method of _io.TextIOWrapper instance
Return current stream position.

seek的第一个参数——cookie——用来指示相对位移,可正可负可零,受限于第二个参数。必需。

seek的第二个参数——whence——用来指示参照物,0(io.SEEK_SET)代表文件开头,1(io.SEEK_CUR)代表当前位置,2(io.SEEK_END)代表文件末尾。默认为0。

 >>> f = open("demo.txt","r")
>>> f.seek(0)
0
>>> f.seek(-1)
Traceback (most recent call last):
File "<pyshell#135>", line 1, in <module>
f.seek(-1)
ValueError: negative seek position -1
>>> f.seek(1)
1

默认/whence=0情况,相对位移只能为零或正数。

 >>> f = open("demo.txt","r")
>>> f.seek(0,1)
0
>>> f.seek(-1,1)
Traceback (most recent call last):
File "<pyshell#126>", line 1, in <module>
f.seek(-1,1)
io.UnsupportedOperation: can't do nonzero cur-relative seeks
>>> f.seek(1,1)
Traceback (most recent call last):
File "<pyshell#127>", line 1, in <module>
f.seek(1,1)
io.UnsupportedOperation: can't do nonzero cur-relative seeks

当前/whence=1情况,相对位移只能为零。这是令人感到奇怪的,原因会在后面讲。

 >>> f = open("demo.txt","r")
>>> f.seek(0,2)
10
>>> f.seek(-1,2)
Traceback (most recent call last):
File "<pyshell#119>", line 1, in <module>
f.seek(-1,2)
io.UnsupportedOperation: can't do nonzero end-relative seeks
>>> f.seek(1,2)
Traceback (most recent call last):
File "<pyshell#120>", line 1, in <module>
f.seek(1,2)
io.UnsupportedOperation: can't do nonzero end-relative seeks

末尾/whence=2情况,相对位移只能为零。这同样令人感到奇怪,原因会在后面讲。

问题

比较help文档的内容和我们前面所得,我们知道whence=1或2时,表现与预期不相符。这是由于打开方式的问题。以“t”方式(文本模式)打开的文件是无法从当前位置或末尾位置开始计算位移的,只允许从开头位置开始计算位移。注意观察上面代码的异常信息。第一段代码中报ValueError异常,而后面的报io.UnsupportedOperation异常。显然异常不同,造成异常的原因也不同。

想要从当前或末尾位置开始计算位移,应当使用“b”方式(二进制模式)打开文件。

 >>> f = open("demo.txt","rb")
>>> f.seek(0)
0
>>> f.seek(-1)
Traceback (most recent call last):
File "<pyshell#106>", line 1, in <module>
f.seek(-1)
OSError: [Errno 22] Invalid argument
>>> f.seek(3)
3
>>> f.seek(0,1)
3
>>> f.seek(-1,1)
2
>>> f.seek(1,1)
3
>>> f.seek(0,2)
10
>>> f.seek(-1,2)
9
>>> f.seek(1,2)
11

最新文章

  1. 使用notepad++进行格式转换
  2. JqueryEasyUI浅谈---视频教程公布
  3. 如何解决php 生成验证码图片不显示问题
  4. 沈逸老师PHP魔鬼特训笔记(4)
  5. hadoop中MapReduce中压缩的使用及4种压缩格式的特征的比较
  6. CUDA_矢量相加
  7. java输入输出流总结 转载
  8. iOS中忽略NSLog打印信息(通过PCH文件中定义DEBUG宏解决)
  9. MySql 跟踪命令
  10. GNU C的使用
  11. CronJobs
  12. Nginx(二)-服务模式运行nginx之WINSW
  13. [vue--开发记录]使用location.href修改地址跳转页面在ie上遇到的坑
  14. node.js http接口调试时请求串行特性分析
  15. 华为4K机顶盒EC6108V9U从原联通更换为电信的IPTV账号成功经验
  16. AJAX的优点 个人理解记录
  17. 记一个神奇的WAS问题:sibuswsgw-sibuswsgw_console.jar invalid LOC header (bad signature) 分类: WebSphere 2015-08-06 23:21 9人阅读 评论(0) 收藏
  18. Python学习笔记第十一周
  19. jieba安装
  20. Spring 通过Java代码装配bean

热门文章

  1. bash脚本中使用选项 getopts
  2. gitolite 代码访问控制
  3. 邻居子系统 之 状态定时器回调neigh_timer_handler
  4. Linux .bashrc
  5. nginx 部署前端项目(vue)
  6. synchronized对象解析
  7. 深度学习之Faster-R-CNN
  8. chrome调试笔记
  9. maven项目无法导入Oracle的jdbc连接jar包【我】
  10. 手写web框架之开发一个类加载器