how to handle null argument in python

1st Scenario
============
Currently have a python script which accepts 1 argument and does something.

process.py nyc:denver:newjercy

Inside my process.py
city_list=sys.argv[1].split(':')





2nd Scenario:
============
Would like to introduce and pass 2nd argument state_list=sys.argv[2].split(':')

e.g
process.py nyc:denver:newjercy ny:ct:ny:co

Inside my process.py
city_list=sys.argv[1].split(':')
state_list=sys.argv[2].split(':')

Upto this stage it works fine.


But now
========
if I dont pass 2nd argument to my process.py it fails with below Error. Which I understand bcuz I'm not passing the argument.
  IndexError: index out of range: 2


  Request is:
  How to handle 2nd argument inside python script. If I pass 'none', is it easy to handle? pls help with sample script.
enthuguyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Duy PhamFreelance IT ConsultantCommented:
I think you can simply use try/except block for 2nd argument, for example:
try:
    state_list=sys.argv[2].split(':')
except IndexError:
    # if 2nd argument is not passed, it goes here
    state_list=<some_default_value>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
enthuguyAuthor Commented:
Thanks for your quick help
0
peprCommented:
I suggest the following
state_list = sys.argv[2].split(':') if len(sys.argv) >= 3 else []

Open in new window


This is called a conditional expression, and works the same way as
if len(sys.argv) >= 3:
    state_list = sys.argv[2].split(':')
else:
    state_list = []

Open in new window

But the conditional expression is better here. It is more dense (fewer lines), the state_list is not repeated (less error-prone as you cannot create two variables by typo error). Actually only the first part of the expression is the important one (this is what should be done); so, you can skip the rest if you are re-reading the source to get the idea.

It does not matter (what solution you choose) too much for getting the sys.argv if the program is short. If there is more complex processing of command-line arguments, then have a look at the argparse standard module (https://docs.python.org/3.5/library/argparse.html).
0
enthuguyAuthor Commented:
Awesome.
Thanks pepr, this is a good one too
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.