Python Command Line Arguments

ian.stegner at ian.stegner at
Wed Apr 12 21:59:00 EDT 2017

I have this code which I got from The example works fine but when I modify it to what I need, it only half works. The problem is the try/except. If you don't specify an input/output, they are blank at the end but it shouldn't be.

import getopt
import sys

def main(argv):
   inputfile = ''
   outputfile = ''
      opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
   except getopt.GetoptError:
      inputfile = 'Input'
      outputfile = 'Output'
   if inputfile == '':
      for opt, arg in opts:
         if opt == '-h':
            print ('Usage: -i <input file> -o <output file>')
         elif opt in ("-i", "--ifile"):
            inputfile = arg
         elif opt in ("-o", "--ofile"):
            outputfile = arg

   print 'In: ' + inputfile
   print 'Out: ' + outputfile

if __name__ == "__main__":

More information about the Python-list mailing list