Python Cookbook 2Nd Edition Jun 1002005 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Python Cookbook 2Nd Edition Jun 1002005 [Electronic resources] - نسخه متنی

David Ascher, Alex Martelli, Anna Ravenscroft

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







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')
record_size = 48
record_number = 6
thefile.seek(record_size * record_number)
buffer = thefile.read(record_size)

Note that the record_number of the
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.


/ 394