stack-like file object

Josiah Carlson jcarlson at
Tue Jan 20 03:43:14 CET 2004


> 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):
        del self.f

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

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

More information about the Python-list mailing list