[Tutor] Unix philosophy
Alan Gauld
alan.gauld at blueyonder.co.uk
Tue Nov 18 12:56:57 EST 2003
> 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?
> Are you thinking to something like this:
> guillecat.py
> # Simple cat UNIX command
> import sys
> if len(sys.argv)==1:
> while(1):
> readed=sys.stdin.read(1)
> sys.stdout.write(readed)
> else:
> for files in sys.argv[1:]:
> filed=file(files,'r')
> readed=filed.read()
> sys.stdout.write(readed)
Exactly so. If no filename is provided assume it's stdin.
Another often seen trick is to have a flag for using as a filter
(Unix tar is an example) so you specify the tar filename as "-f file"
and if the name is '-' use stdin.
tar cvf foo.tar
writes the output to foo.tar
tar cvf -
writes the output to stdout (usually a pipe to another command)
But your way is a perfectly valid way too.
Alan G.
More information about the Tutor
mailing list