randomly write to a file

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon May 7 16:51:07 EDT 2007


En Mon, 07 May 2007 16:51:37 -0300, rohit <rohitsethidce at gmail.com>  
escribió:

> i am developing a desktop search.For the index of the files i have
> developed an algorithm with which
>  i should be able to read and write to a line if i know its line
> number.
> i can read a specified line by using the module linecache
> but i am struck as to how to implement writing to the n(th) line in a
> file EFFICIENTLY
> which means i don't want to traverse the file sequentially to reach
> the n(th) line

You can only replace a line in-place with another of exactly the same  
length. If the lengths differ, you have to write the modified line and all  
the following ones.
If all your lines are of fixed length, you have a "record". To read record  
N (counting from 0):
   a_file.seek(N*record_length)
   return a_file.read(record_length)
And then you are reinventing ISAM.

-- 
Gabriel Genellina




More information about the Python-list mailing list