Ejemplo 1: Python lee argumentos
#!/usr/bin/python
import sys
print('Number of arguments:', len(sys.argv), 'arguments.')
print('Argument List:', str(sys.argv))
Ejemplo 2: python sys.argv
def add(a, b):
return a + b
"""
You need to execute the script add.py as follows:
'python add.py 5 2'
The 'sys.argv[0]' is the name of your script,
so you need to get the second and the third one.
"""
print(add(int(sys.argv[1]), int(sys.argv[2])))
"""
With the command: 'python add.py 5 2',
this python script will returns 7
"""
Ejemplo 3: argv en python
#argv in python
import sys
print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)
#command in cmd :
$ python test.py arg1 arg2 arg3
# producing following result :
Number of arguments: 4 arguments.
Argument List: ['test.py', 'arg1', 'arg2', 'arg3']
Ejemplo 4: función de python argv
# arguments
def test_var_args(f_arg, *argv):
print("first normal arg:", f_arg)
for arg in argv:
print("another arg through *argv :", arg)
test_var_args('yasoob','python','eggs','test')
# keywork arguments
def test_var_kwargs(f_arg, **kwargs):
print(f_arg)
for (key, item) in kwargs.items():
print("Keyword: ", key)
print("Value: ", item)
test_var_kwargs('yasoob', x = 12)
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)