Recipe 2.7. Using Random-Access Input/Output
Credit: Luther Blissett
Problem
You want to read a binary record from
somewhere inside a large file of fixed-length records, without
reading a record at a time to get there.
Solution
The byte offset of the start of a record in the file is the size of a
record, in bytes, multiplied by the progressive number of the record
(counting from 0). So, you can just seek right to the proper spot,
then read the data. For example, to read the seventh record from a
binary file where each record is 48 bytes long:
thefile = open('somebinfile', 'rb')Note that the record_number of the
record_size = 48
record_number = 6
thefile.seek(record_size * record_number)
buffer = thefile.read(record_size)
seventh record is 6: record numbers count from
zero!
Discussion
This approach works only on files (generally binary ones) defined in
terms of records that are all the same fixed size in bytes; it
doesn't work on normal text files. For clarity, the
recipe shows the file being opened for reading as a binary file by
passing 'rb' as the second argument to
open, just before the seek. As
long as the file object is open for reading as a binary file, you can
perform as many seek and read
operations as you need, before eventually closing the file
againyou don't necessarily open the file just
before performing a seek on it.
See Also
The section of the Library Reference and
Python in a Nutshell on file objects;
Perl Cookbook recipe 8.12.