stdin - seek() issue on Linux.

Srihari Vijayaraghavan harisri at bigpond.com
Tue Apr 23 22:16:41 EDT 2002


Hello,
> > Quoth "Terry Reedy" <tejarex at yahoo.com>:
> > | "Srihari Vijayaraghavan" <no at spam.please> wrote in message
> > | news:WMTw8.46875$uR5.106037 at newsfeeds.bigpond.com...
>  
> > |> I am a new-bie python user. I have Python 2.2.1 installed on a Linux
> > |> computer. I get the following error message when I try to seek() in the
> > |> stdin, like:
> > |> #cat /var/log/messages | python test.py
> > |> (where /var/log/messages is over 1MB in size)
> > |>
> > |> Traceback (most recent call last):
> > |>   File "test.py", line 3, in ?
> > |>     sys.stdin.seek(-512, 2)
> > |> IOError: [Errno 29] Illegal seek
> > |
> > | By 'cat'ting the random access disk file, you turned it into a
> > | forward-access-only stream of bytes.  You definitely cannot seek
> > | backwards (and maybe not forward - don't know).  Instead, open() the
> > | file and use the resulting file object:
> > |
> > | msgs = open("/var/log/messages", 'r')
> > | msgs.seek(-512, 2)
> > | ...
> > 
> > Right, though it can be fixed without changing test.py at all -
> > "python test.py < /var/log/messages" will work fine.  This idiom
> > ("cat file | ...") used to regularly receive a UUOC award, "Useless
> > Use Of Cat", from notorious Perl hacker Randall Schwartz when I
> > followed comp.unix.shell a few years back.
> > 
> > Of course if the real program uses some filter, instead of "cat",
> > then there's no solution - you can't seek in a pipe, so the answer
> > has to be "don't do that".
> > 
Once again thanks for your responses. I really appreciate that.

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