Generating generations of files
Eli the Bearded
* at eli.users.panix.com
Mon Apr 29 18:14:39 EDT 2019
In comp.lang.python, Peter J. Holzer <hjp-python at hjp.at> wrote:
> On 2019-04-29 20:12:28 -0000, Grant Edwards wrote:
>> Well, the FILES-11 filesystem on VAX/VMS did that automatically, but
>> that's probably not too helpful.
> 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")
>
> :-)
Noted.
> (WARNING: I haven't really tested this)
No foo:
Traceback (most recent call last):
File "versioned-open", line 21, in <module>
with open11("foo", "w") as f:
File "versioned-open", line 15, in open11
os.unlink(file)
FileNotFoundError: [Errno 2] No such file or directory: 'foo'
There is a foo, but it's not a symlink:
Traceback (most recent call last):
File "versioned-open", line 21, in <module>
with open11("foo", "w") as f:
File "versioned-open", line 9, in open11
oldfile = os.readlink(file)
OSError: [Errno 22] Invalid argument: 'foo'
Parse error on version string:
Traceback (most recent call last):
File "versioned-open", line 21, in <module>
with open11("foo", "w") as f:
File "versioned-open", line 10, in open11
basename, version = oldfile.split(";")
ValueError: not enough values to unpack (expected 2, got 1)
etc (there are several possible parse errors: no semicolon, multiple
semicolons, invalid literal for int()).
That said, I do think it makes a reasonable suggestion for the stated
versioning requirement.
Elijah
------
bet a FAT filesystem would produce a different error
More information about the Python-list
mailing list