stdin - seek() issue on Linux.

Noah noah at noah.org
Tue Apr 23 23:07:07 EDT 2002


It does work like a real file object -- it _IS_ a real file object.
The documentation could be more clear on that point, but
I don't think it's wrong (second paragraph):
	http://www.python.org/doc/current/lib/bltin-file-objects.html
It does say that some file operations may throw an IOException for
	"situations where the operation is not defined for some reason, 
	like seek() on a tty device or writing a file opened for reading."
File-like objects don't have to support all the File methods,
they just have to handle all the methods in some way.

On the other hand, perhaps the File object could be more consistent.
In some cases seek() will throw an IOError exception, but in
other cases it will do nothing (no-op). For example if you
call seek on disk files opened for writing in append mode (mode 'a') 
then seek will be do nothing (no-op)... Hmmm... 
I wonder why it throws an exception in some cases, but does nothing in others.

I prefer the IOError exception. It is actually a good way to handle 
the problem since it alerts you quickly to the semantics of the problem.
Otherwise it could have just failed mysteriously or tried to
fake seek() in some non-intuitive way. For example, they could
have made seek() simply do nothing (no-op), but that would be 
confusing and would require more special cases in the documentation.

Yours,
Noah


-----Original Message-----
>From: python-list-admin at python.org
>[mailto:python-list-admin at python.org]On Behalf Of Srihari Vijayaraghavan
>Sent: Tuesday, April 23, 2002 7:17 PM
>To: python-list at python.org
>Subject: Re: stdin - seek() issue on Linux.
>
>OK. IMHO sys.stdin doesn't work as stated in PLR (Python Library
>Reference), i.e., it doesn't work like a _real_ file object,
>particularly not supporting seek() related operations. While your
>technical explanation sounds OK to me, but I am still not convinced
>why PLR advertises that sys.stdin is treated as a file object. Please
>correct me if I am wrong here (as I am still a newbie user :-).
>
>I have fixed the problem by using cStringIO.StringIO(sys.stdin.read())
>method for now, which I think may not be the most optimised method.
>(as I guess it might read the entire file into the memory, OTOH using
>seek() method I could read only specific part of the file (sys.stdin)
>that I want)
>
>Thanks for your time.
>
>Hari,
>harisri at bigpond.com






More information about the Python-list mailing list