interaction of mode 'r+', file.write(), and file.tell(): a bug or undefined behavior?

Aahz aahz at
Tue Feb 2 00:29:55 CET 2010

In article <4b617f4a$1 at>,
Lie Ryan  <lie.1296 at> wrote:
>f = open('input.txt', 'r+')
>for line in f:
>    s = line.replace('python', 'PYTHON')
>    # f.tell()
>    f.write(s)
>When f.tell() is commented, 'input.txt' does not change; but when
>uncommented, the f.write() succeeded writing into the 'input.txt'
>(surprisingly, but not entirely unexpected, at the end of the file).

Another possible issue is that using a file iterator is generally not
compatible with direct file operations.
Aahz (aahz at           <*>

import antigravity

More information about the Python-list mailing list