Search code examples
pythonfilefile-iolarge-filesrandom-access

Python Random Access File


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 be possible.

Any types or methods would be appreciated.


Solution

  • 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.