This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: open "r+" problem
Type: behavior Stage: resolved
Components: IO Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: otn, zach.ware
Priority: normal Keywords:

Created on 2021-09-01 11:56 by otn, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg400826 - (view) Author: otn (otn) Date: 2021-09-01 11:56
For opened file as "r+", even if write() then readline(), readline() is executed first.

When add tell() after write(), it works correctly.

==========================================
data1 = "aa\nbb\ncc\n"
data2 = "xx\n"

with open("data.txt","w") as f:
    f.write(data1)

with open("data.txt","r+") as f:
    f.write(data2)
#   f.tell()
    print("Line:",repr(f.readline()))

with open("data.txt") as f:
    print("All:",repr(f.read()))
==========================================
OUTPUT:
Line: 'aa\n'
All: 'aa\nbb\ncc\nxx\n'

EXPECTED:
Line: 'bb\n'
All: 'xx\nbb\ncc\n'
msg400843 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2021-09-01 15:26
Usage questions like this are better suited to the Users category at https://discuss.python.org/c/users/7 or the python-list@python.org mailing list.

The write is buffered.  If you want it to be written before you read, add `f.flush()` after the write; `f.tell()` causes an implicit flush.  See also the `buffering` argument to `open`.
msg400872 - (view) Author: otn (otn) Date: 2021-09-01 21:44
It works correct for ver2.6, and also "rb+".
Buffering procsess should work the same as those case.
History
Date User Action Args
2022-04-11 14:59:49adminsetgithub: 89239
2021-09-01 21:44:44otnsetmessages: + msg400872
2021-09-01 15:26:30zach.waresetstatus: open -> closed

nosy: + zach.ware
messages: + msg400843

resolution: not a bug
stage: resolved
2021-09-01 11:56:20otncreate