Last active
December 25, 2015 01:49
-
-
Save aaronsaunders/6897480 to your computer and use it in GitHub Desktop.
Usin argparse with stdin and stdout From http://stackoverflow.com/questions/7862987/consistent-way-to-redirect-both-stdin-stdout-to-files-in-python-using-optparse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import fileinput | |
import sys | |
from argparse import ArgumentParser | |
# https://docs.python.org/dev/library/argparse.html | |
parser = argparse.ArgumentParser(description = """docstring here""") | |
parser.add_argument('-i','--input', | |
help='Input filename', required=True) | |
parser.add_argument('-o','--output', | |
help='Output filename', required=True) | |
parser.add_argument('-l','--log', | |
help='Log filename. The default is stdout') | |
parser.add_argument('-c', '--flag', default= False, | |
help='parameter flag', action='store_true') | |
args = parser.parse_args() | |
with open(args.input, 'r') as fh: | |
lines = fh.readlines() | |
if args.log and args.log != '-': | |
sys.stdout = open(args.log, 'w') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment