Message80230
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. |
|
Date |
User |
Action |
Args |
2009-01-20 00:49:30 | vstinner | set | recipients:
+ vstinner |
2009-01-20 00:49:30 | vstinner | set | messageid: <1232412570.35.0.0224734536413.issue5008@psf.upfronthosting.co.za> |
2009-01-20 00:49:29 | vstinner | link | issue5008 messages |
2009-01-20 00:49:28 | vstinner | create | |
|