Skip to content

file.tell() after a write is not correct in append mode #116376

Open
@isidroas

Description

@isidroas

Bug report

Bug description:

The method tell() returns a lower value than expected. It does not point to the position of the last written byte.

Consider this example:

# appender.py
import sys

file = open(sys.argv[1], 'a+b')
file.seek(0)
file.write(b'56789')
print('offset:', file.tell())
$ printf "01234" > myfile
$ python appender.py myfile
offset: 5

The value should be 10 instead of 5.

Equivalence in C

/* appender.c */
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char **argv){
  if (argc!=2){
    puts("usage ./test file");
    return 1;
  }
  int fd = open(argv[1], O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
  lseek(fd, 0, SEEK_SET);
  write(fd, "56789", 5);
  printf("offset: %ld\n", lseek(fd, 0, SEEK_CUR));
  return 0;
}

which returns the expected value:

$ ./appender
offset: 10

CPython versions tested on:

3.11, 3.12, 3.13

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    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