Python Forum
open('foo','ab+') - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: open('foo','ab+') (/thread-27888.html)



open('foo','ab+') - Skaperen - Jun-25-2020

what does this mode really do in open('foo','ab+')? does it work only beyond the end of an existing file?


RE: open('foo','ab+') - Larz60+ - Jun-26-2020

'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)


RE: open('foo','ab+') - Skaperen - Jun-26-2020

if + means read and write, why can't it be used alone?


RE: open('foo','ab+') - snippsat - Jun-26-2020

(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



RE: open('foo','ab+') - Skaperen - Jun-26-2020

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.


RE: open('foo','ab+') - Larz60+ - Jun-27-2020

it is what it is and has worked for a long time.


RE: open('foo','ab+') - Skaperen - Jun-29-2020

i would have called it 'u' or allowed 'rw'. maybe this is why they didn't let me design it :-)