0

I just want a quick help: How can I pass no value in sys.argv[1] file in python?

For example: For this, my code works and gives the desired result

>python.py Hello
some if-else and for loop
if sys.argv[1] == 'Hello':
    print(something)

But this code doesn't work as I want it to

>python.py
if sys.argv[1] == None:
    print(something)

I don't want to use try and Except block for my program execution, do you guys have a way around for this problem? How can I pass literally no value? Thanks

  • It's impossible that `sys.argv[1]` is `None`, because `sys.argv` is a list of strings. – mkrieger1 Dec 21 '21 at 00:55
  • 1
    If you want to check if the user has passed an argument, you need to check the length of the `sys.argv` list. – mkrieger1 Dec 21 '21 at 00:55
  • @mkrieger1 yes you are right, I tried 'None' and hoped it would work and also tried empty string '' but nothing works. Thankyou for your reply – Aakash Rohila Dec 21 '21 at 00:57
  • @mkrieger1 'If you want to check if the user has passed an argument, you need to check the length of the sys.argv list' I think this logic will work. Thanks :) – Aakash Rohila Dec 21 '21 at 01:00

0 Answers0