stdin - seek() issue on Linux.

Donn Cave donn at u.washington.edu
Mon Apr 22 11:55:55 EDT 2002


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".

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list