[Python-ideas] file API

Amaury Forgeot d'Arc amauryfa at gmail.com
Fri Sep 28 13:39:32 CEST 2012

2012/9/28 MRAB <python at mrabarnett.plus.com>:
> On 2012-09-28 03:07, Greg Ewing wrote:
>> Jim Jewett wrote:
>>> For text files, I would expect it to be a character count rather than
>>> a byte count.  So this particular proposal might end up adding as much
>>> confusion as it hopes to remove.
>> I'm given to understand that the file positions used by
>> the C standard library are supposed to be treated as
>> opaque tokens -- you're not guaranteed to be able to
>> perform arithmetic on them.
> Yet you're allowed to do relative seeks? Does that mean that the file
> position basically works with some undefined units (bytes, characters,
> whatever)?

See the documentation: http://docs.python.org/library/io.html#io.TextIOBase.seek
With text streams, SEEK_CUR and SEEK_END only accept offset=0 (i.e. no
move, or go to EOF)
and SEEK_SET accepts a "cookie" which was returned a previous tell().

This cookie will often look like the absolute file position, but it
also has to contain
the codec status, which will be nontrivial for variable-length encodings.

Amaury Forgeot d'Arc

More information about the Python-ideas mailing list