1import sys
2print("This is the name of the script:", sys.argv[0])
3print("Number of arguments:", len(sys.argv))
4print("The arguments are:" , str(sys.argv))
5
6#Example output
7#This is the name of the script: sysargv.py
8#Number of arguments in: 3
9#The arguments are: ['sysargv.py', 'arg1', 'arg2']
1#!/usr/bin/python
2
3import sys
4
5print('Number of arguments:', len(sys.argv), 'arguments.')
6print('Argument List:', str(sys.argv))
1def add(a, b):
2 return a + b
3
4"""
5You need to execute the script add.py as follows:
6'python add.py 5 2'
7The 'sys.argv[0]' is the name of your script,
8so you need to get the second and the third one.
9"""
10
11print(add(int(sys.argv[1]), int(sys.argv[2])))
12
13"""
14With the command: 'python add.py 5 2',
15this python script will returns 7
16"""
1try:
2 filename = sys.argv[1]
3except IndexError:
4 print "You did not specify a file"
5 sys.exit(1)
6
7
1#argv in python
2
3
4import sys
5
6print 'Number of arguments:', len(sys.argv), 'arguments.'
7print 'Argument List:', str(sys.argv)
8
9#command in cmd :
10$ python test.py arg1 arg2 arg3
11
12# producing following result :
13Number of arguments: 4 arguments.
14Argument List: ['test.py', 'arg1', 'arg2', 'arg3']