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 ...
随机推荐
- 实践001:char 类型字段在表中的长度
Rainy on 20170215 1.同事在 写RFC的时候遇到报错:"YTST_001" 必须为扁平结构.不能将内部表.字符# 原因是自建结构中字段定义为了string 类型. ...
- C#使用 webBrowser 控件总结
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- CentOS7.2 设置GRUB2引导界面分辨率
最近在学习OS引导启动,GRUB2的学习材料也不少,主要还看官方手册清晰些. 公司里办公机的多启动用的ubuntu的界面,还挺炫酷的.之前看其他博客网文里看到可以设置grub2的分辨率,我拿CentO ...
- 如何获取板子上独有的ID号EXYNOS4412/Imx6ul【转】
本文转载自:http://blog.csdn.net/u010871058/article/details/75637175 每个CPU,都有它固定的ID号,ID号就是这个CPU唯一的标识,它可能隐含 ...
- nginx 静态网站配置
/************************************************************************************** * nginx 静态网站 ...
- HEOI2016 树
传送门 这道题还是很简单的,可以树剖,然后还有看大佬暴力模拟AC的????!! 我们就执行俩操作,一个是单点修改,这个随便修,然后就是查询一个点,离他最近的被打过标记过的祖先.这个可以这么想,我们先q ...
- 洛谷 P1315 观光公交 —— 贪心
题目:https://www.luogu.org/problemnew/show/P1315 问题是想不明白改动一条边会对后面造成怎样的影响: 实际上影响的会是一段,当某个车站出发时间受其来人牵制时, ...
- bzoj1138
dp+spfa优化 最朴素的dp是dp[i][j]表示i->j的最短路,然后把所有pair(i,i)放到队列里跑spfa,但是这样被卡掉了,那么我们要优化一下 问题在于每次我们转移的时候要枚举i ...
- 将json文件转换成insert语句的sql文件
引入是要的maven依赖: <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <depend ...
- CodeForces 721B Journey (DP)
题意:给定一个有向图,你从1出发到n,走尽可能多的点,并且使总权值不大于t. 析:在比赛时,竟然看成有向图了,就想了好久,感觉dp,但是不会啊...如果是有向图就好做多了,枚举边,然后打印就好,dp[ ...