语法: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()的更多相关文章

  1. Python3基础 file seek 将文件的指针恢复到初始位置

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  2. file.seek()方法引出的文本文件和二进制文件问题

    问题的起因 菜鸟教程上有一段关于file.seek()方法的讲解,先简短描述一下seek()方法: seek(offset, whence)方法用于移动文件读取指针到指定位置 参数offset--开始 ...

  3. 文件操作中file.seek()方法

    摘要: file.seek()可以将文件游标移动到文件的任意位置,本文具体的file.seek()文件游标移动操作方法. file.seek()方法标准格式是:seek(offset,whence=0 ...

  4. file.seek()/tell()-笔记

    ---------------------------------------------------------------------------------------------------- ...

  5. Python File seek() 方法

    概述 seek() 方法用于移动文件读取指针到指定位置.高佣联盟 www.cgewang.com 语法 seek() 方法语法如下: fileObject.seek(offset[, whence]) ...

  6. Python学习笔记015——文件file的常规操作seek()及tell()

    1 seek() 1.1 概述 file.seek()用于将文件游标移动到文件的任意位置,便于对文件的当前位置(增.删.改.查)操作 1.2 语法 fileObject.seek(offset[, w ...

  7. seek指针大文件上传

    package mainimport (    // "bufio"    "fmt"    "github.com/axgle/mahonia&qu ...

  8. Python学习(16)File(文件)方法

    Python File(文件) 方法 file 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 序号 方法及描述 1 file.close() 关闭文件.关闭后文件不能再进行读 ...

  9. python file operation

    file.open(name[,mode[,buffering]]) 模式的类型有: r 默认只读 w     以写方式打开,如果文件不存在则会先创建,如果文件存在则先把文件内容清空(truncate ...

随机推荐

  1. 编译FreePascal源代码(摘录自邮件询问)

    为了尝试编译FreePascal,我搜了官方文档,并给几位作者都发了邮件询问,目前结果如下: http://wiki.lazarus.freepascal.org/Getting_Lazarus#Co ...

  2. 递归读取制定目录下所有文件夹和文件的实现(java)

    public static String getAllDirectorisAndFiles(String path){ Map<String, Object> responseMap = ...

  3. YTU 2875: 倒霉蛋买饭去

    2875: 倒霉蛋买饭去 时间限制: 1 Sec  内存限制: 128 MB 提交: 22  解决: 17 题目描述 早春星期天的某个早晨,大风呼呼地刮.一个宿舍n个人,谁也不想起床买饭去.他们定了一 ...

  4. Linux设备模型 (1)

    随着计算机的周边外设越来越丰富,设备管理已经成为现代操作系统的一项重要任务,这对于Linux来说也是同样的情况.每次Linux内核新版本的发布,都会伴随着一批设备驱动进入内核.在Linux内核里,驱动 ...

  5. jsp中page指令用法详解

    转自:https://www.jb51.net/article/73428.htm 一.JSP 指令 JSP 指令(directive)影响由 JSP 页面生成的 servlet 的整体结构.下面的模 ...

  6. 基于《Hadoop权威指南 第三版》在Windows搭建Hadoop环境及运行第一个例子

    在Windows环境上搭建Hadoop环境需要安装jdk1.7或以上版本.有了jdk之后,就可以进行Hadoop的搭建. 首先下载所需要的包: 1. Hadoop包: hadoop-2.5.2.tar ...

  7. redis发布(pub)、订阅(sub)模式

    前言:redis提供了很多种功能或模式,可以运用在不同的场景下,今天记录下redis中的发布.订阅模式的基本使用 注redis安装及主从搭建请参考我其他博文http://www.cnblogs.com ...

  8. win7下硬盘安装Windows

    win7下硬盘安装Windows: 1.下载 Windows 7 ISO镜像,用虚拟光驱拷贝至非C盘(如d:\win7)2.开机按F8 - 修复系统 - 选择最后一项命令修复 - 在命令框输入 d:\ ...

  9. 对char类型的数组进行冒泡排序

    package maopaopaixu; import java.util.Arrays; import java.util.Scanner; public class Demo02 { public ...

  10. 12c pdb expdp use DATA_PUMP_DIR meet ORA-39145

    ORA-39002: invalid operation ORA-39070: Unable to open the log file. ORA-39087: directory name DATA_ ...