In Python open (Python docs: open), we have the following modes available:
r - reading only, file must exist.
w - writing only, file will be created if it does not exist, file will be truncated if it does exist.
x - writing only, file must not exist, and will be created.
a - writing only, file will be created if it does not exist, all writes write at end of file.
r+ - reading and writing, file must exist.
w+ - reading and writing, file will be created if it does not exist, file will be truncated if it does exist.
PHP fopen (PHP docs: fopen) supports all of the above, as well as the following additional modes:
x+ - reading and writing, file must not exist, and will be created.
c - writing only, file will be created if it does not exist, file will not be truncated if it does exist.
c+ - reading and writing, file will be created if it does not exist, file will not be truncated if it does exist.
I believe these modes should be added to Python. I previously mentioned this in `open()`able objects - #16 by rrolls in the context of interfaces, and I went into much more detail on my rationale there.
I didn’t previously suggest actually adding it to the builtin open as that was off-topic there, but I was motivated to start this thread today upon running into a use case for c+ mode again.
I’m currently using the following workaround: (but not quite - see my follow-up post below)
import os
with os.fdopen(os.open('file', os.O_CREAT | os.O_RDWR), 'r+b') as f:
...
but I’d really like to be able to write this as simply
with open('file', 'c+b') as f:
...
which should do the equivalent to the above working snippet. c mode should be handled in the same way, but with O_RDONLY instead of O_RDWR. (On Windows, it looks like O_BINARY would also be needed if 'b' is in the mode string.)
As for x+ mode, I’ll quote myself from my earlier post:
x+in Python does not raise an exception if I request it (using builtinopen) but I’ve not tested thoroughly if it actually does what I expect it to do. If Python does already implement this mode, it ought to be documented; otherwise, it should (preferably, IMO) be implemented and documented, or if it’s decided that Python should not support this mode, attempting to use this mode should raise an exception and, again, this should be documented.
(PHP also has a+ mode. I consider this mode quite a footgun, as append mode forces all writes to the end of the file so it doesn’t really make sense (to me) to combine it with the ability to read. Therefore I haven’t listed this one above as I don’t believe it should be added. But if others have a strong desire for it to be added for some reason, I wouldn’t object. It’s also possible, like x+, that it’s already available in Python but not documented, in which case it can stay as it is but ought to be documented.)
Are there any particular objections to these modes being added? (and/or is anyone already working on this?)
If not, I can (at some point) do some more serious testing across Linux and Windows of the existing and newly proposed modes, figure out for sure whether x+ is already implemented, have a stab at implementing it in CPython along with some tests, and suggest some improved docs.