python seek()
2015-11-28 12:06
525 查看
1、seek函数
[plain] view
plaincopyprint?
file.seek(off, whence=0):从文件中移动off个操作标记(文件指针),正往结束方向移动,负往开始方向移动。如果设定了whence参数,就以whence设定的起始位为准,0代表从头开始,1代表当前位置,2代表文件最末尾位置。
2、示例
from sys import argv
script, input_file = argv
def print_all(f):
print f.read()
def rewind(f):
f.seek(0)
def print_a_line(line_count, f):
print line_count, f.readline()
current_file = open(input_file)
print "First let's print the whole file:\n"
print_all(current_file)
print "Now let's rewind, kind of like a tape."
rewind(current_file)
print "Let's print three lines:"
current_line = 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
[plain] view
plaincopyprint?
file.seek(off, whence=0):从文件中移动off个操作标记(文件指针),正往结束方向移动,负往开始方向移动。如果设定了whence参数,就以whence设定的起始位为准,0代表从头开始,1代表当前位置,2代表文件最末尾位置。
2、示例
from sys import argv
script, input_file = argv
def print_all(f):
print f.read()
def rewind(f):
f.seek(0)
def print_a_line(line_count, f):
print line_count, f.readline()
current_file = open(input_file)
print "First let's print the whole file:\n"
print_all(current_file)
print "Now let's rewind, kind of like a tape."
rewind(current_file)
print "Let's print three lines:"
current_line = 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
$ python ex20.py test.txt First let's print the whole file: To all the people out there. I say I don't like my hair. I need to shave it off. Now let's rewind, kind of like a tape. Let's print three lines: 1 To all the people out there. 2 I say I don't like my hair. 3 I need to shave it off. $
相关文章推荐
- python抓取gb2312/gbk编码网页乱码问题
- python培训day5 随笔
- Python基础_即时标记及其相关的语法
- Python基础_正则表达式学习一
- Day-5
- python知识点
- Python多线程编程及同步处理
- python列表总结
- Python import语义
- python解决接口测试获取手机验证码问题
- python 之运维
- 5.1.6 tzinfo对象
- Python篇----面向对象程序设计(三)(继承篇)
- Python篇----面向对象程序设计(二)(属性和方法篇)
- Python2.7下载微信公众号文章的图片(二)
- Python实用小技巧
- Stackless Python 并发式编程介绍
- python3编码
- python第三方库点滴
- 【转】Python包管理工具