New submission from STINNER Victor <victor.stin...@haypocalc.com>:

The following code must display 3 instead of 0:
---
with open("x", "w") as f:
    f.write("xxx")
with open("x", "a") as f:
    print(f.tell())
---

The example works with Python 2.x, because file object is implemented 
using the FILE structure (fopen, ftell, etc.). fopen() "fixes" the 
offset if the file is opened in append mode, whereas open() doesn't do 
this for us :
---
import os
with open("x", "w") as f:
    f.write("xxx")
fd = os.open("x", os.O_RDONLY | os.O_APPEND)
print(os.lseek(fd, 0, 1))
---
display 0 instead of 3 on Python 2.x and 3.x.

It becomes a little bit more weird when you write something :-)
---
with open("x", "w") as f:
    f.write("xxx")
with open("x", "a") as f:
    f.write("y")
    print(f.tell())
---
displays... 4 (the correct position) on Python 2.x and 3.x.

I see (in GNU libc source code) that fopen() call lseek(fd, 0, 
SEEK_END) if the file is opened in append mode.

----------
messages: 80230
nosy: haypo
severity: normal
status: open
title: Wrong tell() result for a file opened in append mode
versions: Python 3.0, Python 3.1

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue5008>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to