Python 文件操作 到达文件某一行

文件对象的基本函数readlines()

适用于文件比较小时,当文件过大,容易出现memoryError错误

with open('primer.txt','r')as f:
    for i in f.readlines():
        print(i,end='')
line1
line2line2
line3line3line3
line4
line5line5line5line5
with open('primer.txt','r')as f:
    print(f.readlines()[4])
line5line5line5line5

linecache库

同样适用于文件比较小时

import linecache
text=linecache.getline('primer.txt',4)
text
'line4\n'

fileObject 迭代器

file.open 函数生成一个迭代器(生成器

利用for循环遍历,可以逐行读取

with open('file','r') as f:
     for line in f:
          print(line)

file.tell()和file.seek()

file.tell()

获取文件指针的位置函数,初始为0

fileObject.seek(offset[, whence])

从文件中移动offset个操作标记(文件指针),正往结束方向移动,负往开始方向移动。
如果设定了whence参数,就以whence设定的起始位为准,0代表从头开始,1代表当前位置,2代表文件最末尾位置。
当 offset 值非 0 时,Python 要求文件必须要以二进制格式打开,否则会抛出 io.UnsupportedOperation 错误。

with open('primer.txt','r')as f:
    print(f.tell())
    f.seek(6)
    print(f.read())
with open('primer.txt','r')as f:
    f.seek(10,0)
    print(f.read())
with open('primer.txt','rb+')as f:#'rb+'
    f.seek(10,0)
    print(f.read())
    f.seek(-10,2)
    print(f.read())
    f.seek(-11,1)
    print(f.read())
0
line2line2
line3line3line3
line4
line5line5line5line5
2line2
line3line3line3
line4
line5line5line5line5
b'2line2\nline3line3line3\nline4\nline5line5line5line5'
b'line5line5'
b'5line5line5'

itertools

itertools 是python的迭代器模块,itertools提供的工具相当高效且节省内存。

  • count(初值=0, 步长=1)#均无限循环
  • islice(count(10), 5)
  • cycle(‘xyz’)
  • dropwhile() 函数起到过滤作用,满足条件的值都会丢弃直到有元素不满足为止
import itertools
with open('primer.txt','r') as f:
    for line in itertools.dropwhile(lambda line: line.startswith('line1'), f):
        print(line, end='') 
line2line2
line3line3line3
line4
line5line5line5line5

Categories: Python

0 Comments

Leave a Reply

Your email address will not be published.