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.

Author vstinner
Recipients vstinner
Date 2009-01-20.00:49:28
SpamBayes Score 9.13631e-10
Marked as misclassified No
Message-id <1232412570.35.0.0224734536413.issue5008@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2009-01-20 00:49:30vstinnersetrecipients: + vstinner
2009-01-20 00:49:30vstinnersetmessageid: <1232412570.35.0.0224734536413.issue5008@psf.upfronthosting.co.za>
2009-01-20 00:49:29vstinnerlinkissue5008 messages
2009-01-20 00:49:28vstinnercreate