python中 file.seek( )和file.readline() 的用法
如新建了一个文本文档test.txtHellopython然后在python中输入>>>file = open ("test.txt", "r")>>>print file.seek(0) #file.seek(0)是重新定位在文件的第0位及开始位置 显示的是None #seek函数不返回值,因此print
·
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
>>>
更多推荐
已为社区贡献1条内容
所有评论(0)