Python Random Access File

前端 未结 7 2130
借酒劲吻你
借酒劲吻你 2020-12-01 16:10

Is there a Python file type for accessing random lines without traversing the whole file? I need to search within a large file, reading the whole thing into memory wouldn\'t

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 17:04

    This seems like just the sort of thing mmap was designed for. A mmap object creates a string-like interface to a file:

    >>> f = open("bonnie.txt", "wb")
    >>> f.write("My Bonnie lies over the ocean.")
    >>> f.close()
    >>> f.open("bonnie.txt", "r+b")
    >>> mm = mmap(f.fileno(), 0)
    >>> print mm[3:9]
    Bonnie
    

    In case you were wondering, mmap objects can also be assigned to:

    >>> print mm[24:]
    ocean.
    >>> mm[24:] = "sea.  "
    >>> print mm[:]
    My Bonnie lies over the sea.  
    

提交回复
热议问题