Prince-linux / python-learning

for learning Python
MIT License
1 stars 0 forks source link

Command-line arguments with argv #16

Open lcabrini opened 9 years ago

lcabrini commented 9 years ago

In the sys module there is a list called argv which gives access to command-line arguments. Actually sys.argv[0] gives you the name of the script. The command-line arguments start from sys.argv[1]. If you want to know the number of elements in argv you can use.

import sys
len(sys.argv)

If you want to know the number of command-line arguments especially you rather use:

len(sys.argv[1:])

This is called slicing, sys.argv[1:] will start from index 1. You can use this to iterate through the arguments with

for a in sys.argv[1:]:

Write a program, called args.py that prints the name of the script, the number of command-line arguments the program received and list the arguments as well. Here is an example of the output you should get:

$ args.py a b c "hello there" 1 2 3
The script name is ./args.py
Got 7 command-line arguments
Here they are:
  1. a
  2. b
  3. c
  4. "hello there"
  5. 1
  6. 2
  7. 3
Prince-linux commented 9 years ago

having difficulty with this issue. Kindly help out please.

lcabrini commented 9 years ago

have you written any code for this? if so, so a commit but without adding closes #16. Instead type ref #16

I'm not sure where you are struggling. Maybe if I see some code I'll know better.