This is a little Python conundrum that I had to solve at work. Hopefully it’ll save someone else some time.
argparse is the fabulous, back ported, standard, built-in Python 2.7 module for parsing command line arguments. Very helpful for building scripts that are a little too complex for
bash. One thing you can do with
argparse is specify that an argument is supposed to be an output (or input) file like so:
[sourcecode language=”python”] parser.add_argument(‘—log’, type=argparse.FileType(‘w’), default=’-‘) [/sourcecode]
argparse has built-in knowledge of the UNIX convention that
- represents standard input or output as appropriate. The above basically defaults to assigning
stdout as the logging target unless a filename is supplied on the command line. Unfortunately, this always overwrites the target file, which is not quite what you want for log files. You really want to just append any new output, not wipe out the old stuff.
But! The following, natural correction, doesn’t quite work, with
argparse throwing an error:
[sourcecode language=”python”] parser.add_argument(‘—log’, type=argparse.FileType(‘a’), default=’-‘) [/sourcecode]
Turns out the
argparse.FileType is hardwired to throw an exception for appending to the
However, there is a solution. You can supply a Python file object for the default, like so:
[sourcecode language=”python”] parser.add_argument(‘—log’, type=argparse.FileType(‘a’), default=sys.stdout) [/sourcecode]
Works great. Defaults correctly, but appends to existing files if specified. Happy hacking!
Also, my first trial with WordPress’ programming language shortcodes. So far so good.