Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Random Access File

Tags:

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.

like image 272
Mantas Vidutis Avatar asked Feb 15 '11 02:02

Mantas Vidutis


People also ask

What is random access file in Python?

Purpose of linecache module in Python's standard library is to facilitate random access to any text file, although this module is extensively used by Python's traceback module to generate error trace stack. Further prettyprints of reading are held in a cache so that it saves time while reading lines repeatedly.

How do you access a file in Python?

Opening a File. You can open a file using the open() function. The open() function takes two arguments - filename and mode. There are different access modes in which you can open a file.


2 Answers

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.  
like image 102
senderle Avatar answered Oct 27 '22 02:10

senderle


You can use linecache:

import linecache
print linecache.getline(your_file.txt, randomLineNumber) # Note: first line is 1, not 0
like image 45
David M. Avatar answered Oct 27 '22 02:10

David M.