A Template for Python Unix Utilities

One of the most often read postings on this blog is the one about my getopts-enabled shell script template. So I figured, a similar template in Python would also be useful. Like the shell script, it's quite trivial, but it might still save some time.

The template is an example for a typical unix utility. It connects two aspects: An optparse-based command line parser and iterating over multiple input sources using the fileinput module.

I've used command line parsers in several programming languages, but from all of them, I liked Python's optparse module best (for those who need it, Python's standard library still provides a classic getopt). The fileinput module is simple but useful. Beyond its main functionality, it supports more advanced features like transparent decompressing (not really Unix-like though) and in-place filtering of files, but I rarely ever use them.

Here's the template for download.

social