[Tutor] how to write a string into a specific line in a file

Alan Gauld alan.gauld at freenet.co.uk
Wed Mar 8 22:25:10 CET 2006


>> Some languages, such as COBOL and some BASICs etc support
>> random access files, unfortunately Python doesn't
> You then go on to mention file.seek().  I thought that seek() provided
> "random access", ie. to seek to anywhere at random in a file.  Can you
> clarify what you mean by "random access files"?

Sure.
The languages I'm describing provide the ability to deftine a data record
and associate that with a file. The file is declared to be of random access
type and this automatically associates an index file with it.

The programmer can then go to specific lines directly, read fields out of
the line, write new lines directly to the file, delete lines out of the file 
etc.
More or less treating the file as if it were a python list of tuples.

The nearest in Python is probably shelve but with the disadvantage that
you can only search on the key rather than on any field in the record.

To see some examples in MALLARD Basic (one of the ones that
I have used in the past - on CP/M!) look here:

http://www.fvempel.nl/basic.html

(Its got German(or Dutch mebbe given the .nl?)  variables and comments
but the keywords are in English!)

Essentially it is possible to create a Python module to do the same, you
just need the index file to hold the byte position for every field of every
record. Inserting lines into the middle is actually done by appending and
just adding a new index entry. Deletions are done by blanking the file
and index entries (maintaining the byte count) Usually a housekeeping
routine will tidy things up when the files are closed.

Could be an interesting excercise. It could even be based on the bsd
database stuff as a starter. The main reason its not popular now is
simply that with massive RAM its usually faster and easier to just read
the data into memory. If its bigger than that a fuill blown database is
a faster option - although several RDBMS are built with ISAM
underneath....

PS. For the really curious you can still buy a commercial version of
Mallard BASIC from its creators Locomotive Software. I'm not clear
whether it is CP/M only or whether they now have a PC version.
But there are PC CP/M emulators! More info from Wikipedia:

http://en.wikipedia.org/wiki/Mallard_BASIC

And for a more coherent explanation of ISAM file access thee is also this:

http://en.wikipedia.org/wiki/ISAM

Ah! 1984 business computing:
BASIC, CP/M, 128K RAM,180K floppies and graphics free text screens...
Happy days! :-)

HTH, Bet you wish you never asked! :-)

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld





More information about the Tutor mailing list