Python-基础-文件操作-随机存取
随机存取
文件操作中,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
Python-基础-文件操作-随机存取的更多相关文章
- python基础-文件操作
一.文件操作 打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作. 打开文件的模式有: r ,只读模式[默认模式,文件必须存在,不存在则抛出异 ...
- python基础-文件操作(10)
一.什么是文件 等等这些都叫做文件,各种格式的.但不仅仅限制于这些. 二.文件的作用 大家应该听说过一句话:“好记性不如烂笔头”. 不仅人的大脑会遗忘事情,计算机也会如此,比如一个程序在运行过程中用了 ...
- Python基础--文件操作和集合
这篇博客来说一下python对文件的操作. 对文件的操作分三步: 1.打开文件获取文件的句柄,句柄就理解为这个文件 2.通过文件句柄操作文件 3.关闭文件. 现有以下文件file.txt: 我们哭了 ...
- python 基础文件操作
实时刷新到硬盘里 f= open('hh','w',encoding='utf8') f.write('gyftyftft') f.write('hghgh\njkkjk') f.flush()#实时 ...
- Python 基础 文件操作
字符串与字节之间的转换 # utf-8 一个汉字 占三个字节 # gbk 一个汉字 占两个字节 # 字符串转换成字节 print(bytes('汉字', encoding='utf-8'))print ...
- python基础--文件操作实现全文或单行替换
python修改文件时,使用w模式会将原本的文件清空/覆盖.可以先用读(r)的方式打开,写到内存中,然后再用写(w)的方式打开. 替换文本中的taste 为 tasting Yesterday whe ...
- Python基础————文件操作
文件操作 4.1 文件基本操作 obj = open('路径',mode='模式',encoding='编码') # 表示要干嘛 读 还是写 obj.write() #写什么内容 obj.read() ...
- Python基础—文件操作(Day8)
一.文件操作参数 1.文件路径 1)绝对路径:从根目录开始一级一级查找直到找到文件. f=open('e:\文件操作笔记.txt',encoding='utf-8',mode='r') content ...
- python基础 — 文件操作
读取键盘输入 Python提供了两个内置函数从标准输入读入一行文本,默认的标准输入是键盘.如下: raw_input input raw_input函数 raw_input([prompt]) 函数从 ...
- Python基础-文件操作(七)
一.文件基本操作 1.open 打开模式: w模式 写模式write 文件不存在时会创建文件,如果文件已存在则会清空文件 r模式 读模式read 文件不存在就报错,存在则准备读取文件 a模式 追加模式 ...
随机推荐
- jQuery动画之显示隐藏动画
1. 显示动画 以下面一个代码示例: <!doctype html> <html lang="en"> <head> <meta char ...
- dcokee 安装 nginx
1,docker pull openresty/openresty 2, mkdir /opt/local/openresty 等等文件夹 3, docker run --name=" ...
- 6.使用Feign实现声明式REST调用
使用Feign实现声明式REST调用 6.1. Feign简介 Feign是一个声明式的REST客户端,它的目的就是让REST调用更加简单. Feign提供了H ...
- Linux下出现Permission denied解决
今天不想写前言,直接写解决办法 输入命令设置root密码 sudo passwd 得到的答复是 We trust you have received the usual lecture from th ...
- 0ctf-ezdoor-复现分析
在学习opcache的时候,看到了这个题目,刚好有环境,就来复现一下,这个题目也让我学到了很多. 创建镜像: docker build -t 0ctf-ezdoor . 启动容器: docker ru ...
- 获取当前页面的webview ID
代码: A页面 <script type="text/javascript"> var ws = null; mui.plusReady(function(){ ws ...
- vue中解决three.js出现内存泄漏丢失上下文问题
在跳转页面时添加以上代码即可. 在spa项目中,跳转页面并不会清楚已经创建的webgl实例,需要手动清楚.
- js回调函数(callback)(转载)
学习jquery时,对回调函数感觉很困惑,在晚上找了半天,忽然发现这篇文章很浅显,基本说明了问题.故转载 原文: 自学jquery的时候,看到一英文词(Callback),顿时背部隐隐冒冷汗.迅速go ...
- Android View的加载流程
什么是Activity? Activity是 用户操作的可视化界面:它为用户提供了一个放置视图和交互操作的窗口.采用setContentView的方法提供.因此,可以理解Activity.Window ...
- 浏览器输入url后发生的事情以及每步可以做的优化
首先总结下输入url按下回车后的大致流程: 查询url的ip地址. 建立tcp连接,连接服务器. 浏览器发起http/https请求. 服务器响应浏览器的请求. 网页的解析与渲染. 下面分析每个过程 ...