[Tutor] Unix philosophy

A.M. Kuchling amk at amk.ca
Tue Nov 18 07:17:40 EST 2003


On Tue, Nov 18, 2003 at 12:23:49PM +0100, Guillermo Fernandez Castellanos wrote:
> That means that if I wanted to do it in a more general way that with 
> fileinput module I should use the sys.stdin/out functions?

I would write it so that the library function takes any iterable for the
input, and any object with a .write() method for output. Callers could then
supply a file object, sys.stdin, or some constructed object of their own.
For example:

def process (input, output):
    for line in input:
        ...
	output.write(line)
 
if __name__ ==  '__main__':
    import sys
    # (Or process command-line arguments to specify files...)
    process(sys.stdin, sys.stdout)

--amk



More information about the Tutor mailing list