[Python-3000] On PEP 3116: new I/O base classes

Christian Heimes lists at cheimes.de
Wed Jun 20 17:32:27 CEST 2007


Bill Janssen wrote:
> Good point.  Though I just grepped all my Python sources, and I never
> do that, so presumably the obvious workaround of

I'm using seek(0, 2) + tell() sometimes when I need to know the file
size and don't want to worry about buffers.

pos = fd.tell()
size = None
try:
    fd.seek(0, 2)
    size = fd.tell()
finally:
    fd.seek(pos)

IMO you made a good point. The seek() arguments are really too UNIX
centric and hard to understand for newbies. The os module contains three
aliases for seek (SEEK_CUR, SEEK_END, SEEK_SET) (why is it called SET
and not START?) but they are rarely used.

What do you think about adding two additional functions which act as
alias from whence = 1 and whence = 2?

    def seek(self, pos: int, whence: int = 0) -> int:
        """Change stream position.

        Seek to byte offset pos relative to position indicated by whence:
             0  Start of stream (the default).  pos should be >= 0;
             1  Current position - whence may be negative;
             2  End of stream - whence usually negative.
        Returns the new absolute position.
        """

    def seekcur(self, pos: int) -> int:
        """seek relative to current position

        alternative names: seekrel, seek_relative
        """
        return self.seek(pos, 1)

    def seekend(self, pos: int) -> int:
        """seek from end of stream

        alternative names: seekeof
        """
        return self.seek(pos, 2)




More information about the Python-3000 mailing list