[Tutor] Unix philosophy

Alan Gauld alan.gauld at blueyonder.co.uk
Tue Nov 18 06:16:46 EST 2003


> I was having a look at the UNIX philosophy and I readed that:
> "Make every program a filter
> I was wondering how I could be able to make my python programs work
in a
> filter mode?

A filter in Unix is a program that reads its input from stdin
and writes its output to stdout (and ideally its errors to stderr)

This allows us to build up commands as sequences(called pipelines)
like:

cat foo.txt | sort | uniq

Which sends(cat) the file foo.txt into sort which sends
the sorted result into uniq which removes duplicates.
The final output being a list of the unique words in foo.txt.

So how do we write such a program in Python?

In Python raw_input and input both read from stdin by default and
print writes to stdout. BUt raw_input waits for the input line by
line, to be a true Unix filter there should be no prompts or waiting,
so instead of raw_input we need to read() from sys.stdin...

What it means about files is that ideally you shouldn't
write a program that does this:

fname = raw_input("Whats your filename?")
text = file(fname).read()

rather you should do:

text = sys.stdin.read()

And rely on the previous command in the pipeline(possibly cat)
to send the file content to you.

If the program is not part of a pipeline it will just sit and
wait for you to type input and read it until you type EndOfFile
(Ctrl-D in Unix).

This is a bit unfriendly so its common to write programs that
take a command line argument which switches filter behavious
on or off. There is no fixed convention for how this is done.

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list