1import sys
2
3def hello(a,b):
4 print "hello and that's your sum:", a + b
5
6if __name__ == "__main__":
7 a = int(sys.argv[1])
8 b = int(sys.argv[2])
9 hello(a, b)
10# If you type : py main.py 1 5
11# It should give you "hello and that's your sum:6"
1#!/usr/bin/python
2
3import sys
4
5print 'Number of arguments:', len(sys.argv), 'arguments.'
6print 'Argument List:', str(sys.argv)
7
8#Terminal
9# $ python test.py arg1 arg2 arg3
10
11#print
12#Number of arguments: 4 arguments.
13#Argument List: ['test.py', 'arg1', 'arg2', 'arg3']
14
15
1#!/usr/bin/python
2
3import sys, getopt
4
5def main(argv):
6 inputfile = ''
7 outputfile = ''
8 try:
9 opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
10 except getopt.GetoptError:
11 print 'test.py -i <inputfile> -o <outputfile>'
12 sys.exit(2)
13 for opt, arg in opts:
14 if opt == '-h':
15 print 'test.py -i <inputfile> -o <outputfile>'
16 sys.exit()
17 elif opt in ("-i", "--ifile"):
18 inputfile = arg
19 elif opt in ("-o", "--ofile"):
20 outputfile = arg
21 print 'Input file is "', inputfile
22 print 'Output file is "', outputfile
23
24if __name__ == "__main__":
25 main(sys.argv[1:])