student.txt
hello world
word ppt excel
读取一行
f = open("student.txt","r+")
print(f.readline())##读取一行
f.close()
hello world
读取每一行并且存入列表中
f = open("student.txt","r+")
print(f.readlines())##读取每一行并且存入列表中
f.close()
['hello world\n', 'word ppt excel\n']
读取的多少位
f = open("student.txt","r+")指针的移动
print(f.read(3))##读取的位
f.close()
hel
f = open("student.txt","r+")
f.seek(2)##将指针运动了两个位置
print(f.read())
f.close()
llo world
word ppt excel
注意
-
指标读到哪里下次就从哪里读取
-
Python3.0后只有是以b二进制的读取才能完整使用指标
f = open("student.txt","r+")
print(f.readline())##读取了第一行的内容,指标在第一行的d
##记得空行也被读取了出来
print(f.readlines())##因为指标在最后一行所以读取只能读取返回'word ppt excel\n'
f.close()
hello world
['word ppt excel\n']