Skip to content

file.seek(file.tell()) needed: bad docs or a bug? #93079

Open
@Akuli

Description

@Akuli

The following produces a file containing b'hello\n ', i.e. hello followed by a newline and a space:

with open("asd.txt", "w") as file:
    file.write("hello\n")

with open("asd.txt", "r+") as file:
    file.read(4)
    file.write(" ")

But if I add file.seek(file.tell()), then it instead produces b'hell \n' as I would expect:

with open("asd.txt", "w") as file:
    file.write("hello\n")

with open("asd.txt", "r+") as file:
    file.read(4)
    file.seek(file.tell())
    file.write(" ")

Is this correct behaviour? I can't find any mention of it in the docs. (I can't use file.seek(4) because that "produces undefined behaviour" according to docs.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)topic-IOtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions