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 ...
随机推荐
- Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content
Content-Type https://tools.ietf.org/html/rfc7231#section-3.1.1.5 https://tools.ietf.org/html/rfc7231 ...
- 设计模式-(10)观察者模式 (swift版)
一,概念 观察者(Observer)模式又名发布-订阅(Publish/Subscribe)模式.GOF给观察者模式如下定义:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它 ...
- leetcode 660. Remove 9
Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... So now, you will have ...
- MARGIN-BEFORE MARGIN-AFTER MARGIN-START MARGIN-END
总的来说:这是CSS3.0的对于文章段P容器的定义方法语句!display:block这个样式,只定义了P容器为一个块;后面四句是CSS3中的样式定义方法:-webkit-margin-before: ...
- Linux IO多路复用之epoll网络编程(含源码)
前言 本章节是用基本的Linux基本函数加上epoll调用编写一个完整的服务器和客户端例子,可在Linux上运行,客户端和服务端的功能如下: 客户端从标准输入读入一行,发送到服务端 服务端从网络读取一 ...
- nginx中查看关于php的配置和php-fpm的重启等操作
1.查看当前使用的php的配置信息 在php项目的根目录下新建findini.php文件,内容如下: <?php phpinfo(); ?> 然后在页面上访问就可以看到如下页面: 搜索Lo ...
- 【189】◀▶ PowerShell 系统学习
参考网站如下: PowerShell 中文博客 PowerShell 博客——叹为观止 Mater-PowerShell 通过 PowerShell 编写脚本 Power ...
- 我的JSP中文编码解决方案
虽然以前就知道编码问题,但是一直没有遇到问题,以前用asp.net和php的时候,感觉很自然地写程序,没怎么特别处理编码问题,这回改用java写,真心被恶心到了. 进行了一番查阅学习后,终于搞明白了一 ...
- myeclipse 导入maven
一安装maven 先安装jdk,配置JAVA_HOME 把下载的maven bin包,解压到指定目录,比如:D:\apache-maven-3.3.9-bin 配置maven的系统变量M2_HOME和 ...
- QT笔记-布局
1 QT中使用布局器QLayout布局 2自动计算各个空间的大小和位置 采用的既定policy策略来调整子窗口的大小和位置 3QHBoxLayout横向布局 QVBoxLayout纵向布局 QHBo ...