python中 file.seek( )和file.readline() 的用法

file.readline()读取文件的一行,然后将“指针”移动到\n的后面。
file.seek():
如新建了一个文本文档test.txt
Hello  python
然后在python中输入
>>>file = open ("test.txt", "r")
>>>print file.seek(0)		        #file.seek(0)是重新定位在文件的第0位及开始位置 
显示的是None                          #seek函数不返回值,因此print后显示为None


file.seek()是一个类似于索引的用途
file = open("test.txt","r")          
file.read()                                 #读出源文本中内容
file.seek(2)                               #定位到第2个位置后(hello 的e处)
for i in file:
    print i                                    #现在到了最后一位了
for i in file:
    print i                                    #不会显示任何结果(仅出现提示符>>>)
file.seek(0)                               #定位到第0个(重新定位到0的好处是不用再次打开文件。)
for i in file:
    print i 

 
运行结果:
Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> file = open("test.txt","r")
>>> file.read()
'hello python'
>>> file.seek(2)
>>> for i in file:
	print i

llo python
>>> for i in file:
	print i
>>> file.seek(0)
>>> for i in file:
	print i

hello python
>>> 
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值