「python」文件的随机读写
来源:互联网 发布:淘宝查号131458come 编辑:程序博客网 时间:2024/05/22 10:40
文件的随机读写
获取当前读写的位置
在读写文件的过程中,如果想知道当前的位置,可以使用tell()来获取
# 打开一个已经存在的文件 f = open("test.txt", "r") str = f.read(3) print "读取的数据是 : ", str # 查找当前位置 position = f.tell() print "当前文件位置 : ", position str = f.read(3) print "读取的数据是 : ", str # 查找当前位置 position = f.tell() print "当前文件位置 : ", position f.close()
定位到某个位置
如果在读写文件的过程中,需要从另外一个位置进行操作的话,可以使用seek()
seek(offset, from)有2个参数
- offset:偏移量
- from:方向
- 0:表示文件开头
- 1:表示当前位置
- 2:表示文件末尾
demo:把位置设置为:从文件开头,偏移5个字节
# 打开一个已经存在的文件 f = open("test.txt", "r") str = f.read(30) print "读取的数据是 : ", str # 查找当前位置 position = f.tell() print "当前文件位置 : ", position # 重新设置位置 f.seek(5,0) # 查找当前位置 position = f.tell() print "当前文件位置 : ", position f.close()
demo:把位置设置为:离文件末尾,3字节处
# 打开一个已经存在的文件 f = open("test.txt", "r") # 查找当前位置 position = f.tell() print "当前文件位置 : ", position # 重新设置位置 f.seek(-3,2) # 读取到的数据为:文件最后3个字节数据 str = f.read() print "读取的数据是 : ", str f.close()
阅读全文
0 0
- 「python」文件的随机读写
- 文件的随机读写
- ******************* 文件的随机读写实现 *******************
- Java 文件的随机读写
- 「python」文件的读写
- Python文件的读写
- python文件的读写
- Python 文件的读写
- python文件的读写
- Python文件的读写
- Python的读写文件
- 【Python】文件的读写
- python文件的读写
- RandomAccessFile类的使用,随机读写文件。
- 应用:实现文件的随机读写
- C语言文件的随机读写
- 127 C语言文件的随机读写
- C语言文件的随机读写
- 4GQoS测速应用总结(二)
- informatica中Mapping Variable修改
- 继承和多态练习
- ecos tcpip代码解析
- Hadoop学习笔记 5
- 「python」文件的随机读写
- 1+1?
- BZOJ 1706: [usaco2007 Nov]relays 奶牛接力跑 floyd矩阵乘法
- 线程的五大状态
- DNS 禁用权威域名服务器递归解析
- Kaggle入门:Digit Recognizer
- maven+Spring+SpringMVC+mybatis+mybatis-generator 搭建SSM开发环境
- javascript:with的用法以及延长作用域链
- Kettle Spoon入门教程