[Python-ideas] file API

Philip Jenvey pjenvey at underboss.org
Thu Sep 27 22:59:08 CEST 2012


On Sep 27, 2012, at 1:07 PM, MRAB wrote:

> On 2012-09-27 20:40, Jim Jewett wrote:
>> On 9/24/12, Mark Adam <dreamingforward at gmail.com> wrote:
>>> For some time now, I've wanted to suggest a better abstraction for the
>>> <file> type in Python.  It currently uses an antiquated, low-level
>>> C-style interface for moving around in a file, with methods like
>>> tell() and seek().
>> 
>> I agree, but I'm not sure the improvement can be *enough* of an
>> improvement to justify the cost of change.
>> 
>>>>>> file.pos = x0ae1       #move file pointer to an absolute address
>>>>>> file.pos += 1             #increment the file pointer one byte
>> 
>> 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.
>> 
> In the talk about how to seek to the end of the file with file.pos, it
> was suggested that negative positions and None could be used.
> 
> I wonder whether they could be used with seek. For example:
> 
> file.seek(-10)  # Seek 10 bytes from the end.
> file.seek(None) # Seek to the end.

file.seek(0, os.SEEK_END) is a lot clearer than file.seek(None).

--
Philip Jenvey




More information about the Python-ideas mailing list