Generating generations of files

Peter J. Holzer hjp-python at hjp.at
Mon Apr 29 17:04:21 EDT 2019


On 2019-04-29 20:12:28 -0000, Grant Edwards wrote:
> On 2019-04-29, DL Neil <PythonList at DancesWithMice.info> wrote:
> > Are you aware of a library/utility which will generate and maintain the 
> > file names of multiple generations of a file?
> 
> Well, the FILES-11 filesystem on VAX/VMS did that automatically, but
> that's probably not too helpful. Though I guess Python is actually
> available for OpenVMS, and the porting of OpenVMS to AMD64 (aka
> x86-64) is progressing: https://www.vmssoftware.com/updates_port.html

Until this is finished you could use something like this:

#!/usr/bin/python3

import os

def open11(file, mode, **kwargs):
    if "w" in mode:
        try:
            oldfile = os.readlink(file)
            basename, version = oldfile.split(";")
        except FileNotFoundError:
            basename = os.path.basename(file)
            version = 0
        newfile = basename + ";" + str(int(version) + 1)
        os.unlink(file)
        os.symlink(newfile, file)
    return open(file, mode, **kwargs)


if __name__ == "__main__":
    with open11("foo", "w") as f:
        f.write("test1")

    with open11("foo", "w") as f:
        f.write("test2")

    with open11("foo", "w") as f:
        f.write("test3")

:-)

(WARNING: I haven't really tested this)

	hp

PS: I like Chris' suggestion to just use git. But it's IMHO only
practical if you plan to keep old versions for ever.

-- 
   _  | Peter J. Holzer    | we build much bigger, better disasters now
|_|_) |                    | because we have much more sophisticated
| |   | hjp at hjp.at         | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20190429/f763e60d/attachment.sig>


More information about the Python-list mailing list