Python Forum

Full Version: open('foo','ab+')
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
what does this mode really do in open('foo','ab+')? does it work only beyond the end of an existing file?
'a': file pointer at end of file upon opening
'b': binary mode
'+': read and write
you can read from any point in file, but will have to use fseek to get to front of data you want to read,
and fseek aferwards to return to end of file
data can only be written at end of file (Not absolutely sure on this, but think that's correct)
if + means read and write, why can't it be used alone?
(Jun-26-2020, 05:23 PM)Skaperen Wrote: [ -> ]if + means read and write, why can't it be used alone?
Because they have written it so + don't work alone.
I guess no need for one mode for + alone,as r w a in combination with + make more sense.
If look at source line 323
case '+':
    if (plus)
        goto bad_mode;
So it goes to bad mode.
>>> open('foo.txt', '+')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
ValueError: Must have exactly one of create/read/write/append mode and at most one plus
i'm just seeing it called "read/write". that makes me think it would have read and write modes at the same time. since you can't do 'rw', it would seem like '+' is there to do that. but you have to pick one of 'r' or 'w' to append '+' to really have both.
it is what it is and has worked for a long time.
i would have called it 'u' or allowed 'rw'. maybe this is why they didn't let me design it :-)