file.seek()
语法:fileObject.seek(offset,whence)
- offset -- 开始的偏移量,也就是代表需要移动偏移的字节数
- whence:可选,默认值为 0。给offset参数一个定义,表示要从哪个位置开始偏移;0代表从文件开头开始算起,1代表从当前位置开始算起,2代表从文件末尾算起。
'1,2'模式需要'b'二进制模式打开文件,否则报错。
foo.txt
This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
下面过程是连贯的,我插入|引用做结果显示
fo=open('foo.txt','r+')
#使用seek(),tell(),truncate()等功能须用'读写'模式。
print('filename:',fo.name)
line1=fo.readline()
print('line1:',line1)
pos=fo.tell()
print('the pos is>%s'%pos)
$ py seek.py foo.txt
filename: foo.txt
line1: This is 1st line
the pos is>18
fo.seek(0) #指针定位文件首位
line1_1=fo.readline()
print('line1_1:',line1_1)
pos=fo.tell()
print('the pos is>%s'%pos)
line1_1: This is 1st line
the pos is>18
print('filename:',fo.name)
fo.seek(3,0)#指针定位文件首位,向后偏移3个字符
line1_2=fo.readline()
print('line1_2:',line1_2)
pos=fo.tell()
print('the pos is>%s'%pos)
line1_2: s is 1st line
the pos is>18
#使用seek(x,1/2)模式定位,须使用带有'b'二进制模式.
#fo=open('foo.txt','rb+')
fo.seek(5,1) #从当前位置(接上,即'∧This is 2nd line')向后偏移5个字符
line2=fo.readline()
print('line2:',line2)
pos=fo.tell()
print('the pos is>%s'%pos)
line2: b'is 2nd line\r\n'
the pos is>36
#fo=open('foo.txt','rb+')
fo.seek(-4,2) #'2'定位尾部,偏移量为负值,向前偏移4个字符.
line3=fo.readline()
print('line3:',line3)
pos=fo.tell()
print('the pos is>%s'%pos)
line3: b'line'
the pos is>88
file.seek()的更多相关文章
- Python3基础 file seek 将文件的指针恢复到初始位置
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- file.seek()方法引出的文本文件和二进制文件问题
问题的起因 菜鸟教程上有一段关于file.seek()方法的讲解,先简短描述一下seek()方法: seek(offset, whence)方法用于移动文件读取指针到指定位置 参数offset--开始 ...
- 文件操作中file.seek()方法
摘要: file.seek()可以将文件游标移动到文件的任意位置,本文具体的file.seek()文件游标移动操作方法. file.seek()方法标准格式是:seek(offset,whence=0 ...
- file.seek()/tell()-笔记
---------------------------------------------------------------------------------------------------- ...
- Python File seek() 方法
概述 seek() 方法用于移动文件读取指针到指定位置.高佣联盟 www.cgewang.com 语法 seek() 方法语法如下: fileObject.seek(offset[, whence]) ...
- Python学习笔记015——文件file的常规操作seek()及tell()
1 seek() 1.1 概述 file.seek()用于将文件游标移动到文件的任意位置,便于对文件的当前位置(增.删.改.查)操作 1.2 语法 fileObject.seek(offset[, w ...
- seek指针大文件上传
package mainimport ( // "bufio" "fmt" "github.com/axgle/mahonia&qu ...
- Python学习(16)File(文件)方法
Python File(文件) 方法 file 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 序号 方法及描述 1 file.close() 关闭文件.关闭后文件不能再进行读 ...
- python file operation
file.open(name[,mode[,buffering]]) 模式的类型有: r 默认只读 w 以写方式打开,如果文件不存在则会先创建,如果文件存在则先把文件内容清空(truncate ...
随机推荐
- git 团队代码管理交流共同进步
Installation methods for GitLab | GitLab https://about.gitlab.com/installation/#centos-7 gittutorial ...
- Node.js 101(2): Promise and async
--原文地址:http://blog.chrisyip.im/nodejs-101-package-promise-and-async 先回想一下 Sagase 的项目结构: lib/ cli.js ...
- nextLine() 、nextInt()的跳过问题
nextInt() 将输入的信息的下一个标记扫描为 int. nextLine() 此扫描器执行当前行,并返回跳过的输入信息. nextInt 会读取下面输入的 int类型的信息以回车作为结束,如果 ...
- Lightoj 1025 - The Specials Menu
区间dp /* *********************************************** Author :guanjun Created Time :2016/6/30 23:2 ...
- HDU1257 最少拦截系统 —— 贪心
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1257 最少拦截系统 Time Limit: 2000/1000 MS (Java/Othe ...
- YTU 2596: 编程题B-日期格式
2596: 编程题B-日期格式 时间限制: 1 Sec 内存限制: 128 MB 提交: 981 解决: 74 题目描述 注:本题只需要提交编写的函数部分的代码即可. 将输入的日期格式 月/日/年 ...
- YTU 2838: 改错题AB-装置连接
2838: 改错题AB-装置连接 时间限制: 1 Sec 内存限制: 128 MB 提交: 81 解决: 49 题目描述 注:本题只需要提交标记为修改部分之间的代码,请按照C++方式提交. 有AB ...
- AutoIT: ControlSetText
1. ControlSetText :可以摆脱Send的限制,在适当的文本框位置输入用户想要输入的信息.2. ControlGetText可以获取文本 Run("notepad.exe&qu ...
- bzoj 2006 超级钢琴 —— ST表
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2006 本来应该是可以用主席树,找区间最小值,取出来后再找那段区间的次小值...... 但也可 ...
- Ruby Encoding类
Encoding类 内部编码 IO对象内部处理时候的编码 外部编码 IO对象对外输出的时候的编码 输入 外部字符与自己的外部编码对比(没设定的默认 ...