Long Live Python!

Terry Reedy tjreedy at home.com
Thu Jul 12 16:31:51 EDT 2001


"Alex Martelli" <aleaxit at yahoo.com> wrote in message
...
> import os
> os.system('cat file*1 file*2 file*3 | tee file4 | wc -cwl >> file5')

So, by putting 'import os' in site.py, we can say that any one-liner for
the local default command processor can be turned into an identally
functioning Python one-liner by wrapping it in a os.system call.
...
>Most  likely, one would program it differently, solidly, and portably:
>
> import fileinput
> from glob import glob
>
> file4 = open('file4','w')
> lines = words = chars = 0
> for line in
fileinput.input(glob('file*1')+glob('file*2')+glob('file*3')):
>     file4.write(line)
>     lines += 1
>     words += len(line.split())
>     chars += len(line)
> file4.close()
> file5 = open('file5','a')
> file5.write("%s lines, %s words, %s chars\n"%(lines,words,chars))
> file5.close()

While more verbose, this is *much* more flexible.  We could count sentences
instead of lines, redefine 'word', count subsets of chars, format the
output however desired, and stick the results on a remote website (can sh
do this last now?).  We can also to n-way instead of just 2-way splits of
the data stream, or re-merge data streams.  And so on ...

Terry J. Reedy






More information about the Python-list mailing list