stack-like file object

Josiah Carlson jcarlson at uci.edu
Mon Jan 19 21:43:14 EST 2004


Rodrigo,

> I need to implement a stack on a file.
> The idea is to have a big list, and put part of his head on the disk. The
> model of access to the file is like a stack (read in order only the tail,
> write only at the tail).
> How could I create this, knowing that I need to pickle arbritary objects ?

While I don't condone the use of files as stacks, the below should work
for you.  It doesn't reduce in size when an object is removed, but as
long as you don't try to share the stack between processes or threads,
it should work fine.

 - Josiah

import cPickle
import struct
import os

class filestack:
    def __init__(self, filename):
        try:    os.remove(filename)
        except: pass
        self.fn = filename
        self.f = open(filename, 'wb+')
        self.s = len(struct.pack('!i', 0))

    def __del__(self):
        self.f.close()
        del self.f
        os.remove(self.fn)

    def append(self, data):
        st = cPickle.dumps(data)
        self.write(st)
        self.write(struct.pack('!i', len(st)))

    def pop(self):
        posn = self.f.tell()
        if posn <= 0:
            raise IndexError
        self.f.seek(posn-self.s)
        s = struct.unpack('!i', self.f.read(self.s))
        self.f.seek(posn-self.s-s)
        ret = cPickle.loads(self.f.read(s))
        self.f.seek(posn-self.s-s)
        return ret





More information about the Python-list mailing list