[Tutor] Simple question on creating a filter

Smith, Jeff jsmith at medplus.com
Fri Feb 11 15:28:35 CET 2005


I'm sorry to both with such a simple question but I've looked in the
normal places and don't see the quick and dirty answer I know must
exist.

I want to write a simple line selection filter that could be used like:

filter < file

In Perl I would do:

while (<>)
{
	print if line meets selection criteria;
}

I've tried what I thought would be the Python equivalent but it doesn't
work:

for line in sys.stdin:
	if line meets selection criteria:
		print line

I get the following at runtime:

Traceback (most recent call last):
  File "U:\TimeKeeper\mine.py", line 2, in ?
    for line in sys.stdin:
IOError: [Errno 9] Bad file descriptor

This is with Python 2.4 on Windows.

Jeff


More information about the Tutor mailing list