Skip to content

Instantly share code, notes, and snippets.

@moriarty
Created May 29, 2013 20:34
Show Gist options
  • Save moriarty/5673604 to your computer and use it in GitHub Desktop.
Save moriarty/5673604 to your computer and use it in GitHub Desktop.
python get some data from file via command line arg
#!/usr/bin/env python
import numpy as np
import sys
import argparse
from argparse import ArgumentParser
import os
def extant_file(x):
"""
'Type' for argparse -checks that the file exists
"""
if not os.path.exists(x):
raise argparse.ArgumentError("{0} does not exist".format(x))
return x
def main(argv):
## Parse the options
parser = ArgumentParser()
#parser.add_argument("-h", "--help")
parser.add_argument("-i", "--input",
dest="filename", required=True, type=extant_file,
help="input file", metavar="FILE")
args = parser.parse_args()
## Load Data
# in this example the first line contains data that
# was different from the rest of the data.
with open(args.filename,'r') as f:
l1 = f.readline() # Grab the first line
l1 = l1.split() # readline returned a string
l1 = [np.float(l1[0]), np.float(l1[1])] # array from l1
data = np.loadtxt(f) # Grab the data from rest of file
if __name__ == '__main__':
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment