Answers for "check if argv exists python"

0

check if argv exists python

# First, make sure that you import "sys"
import sys

# Next, run a simple conditonal statement to check if argv exists
if len(sys.argv) > 1:
    result = sys.argv[1]
else:
    result = False

# Finally, you can use the result from the above test and run further code accordingly
if result == False:
    print("argv DOES NOT exist!")
else:
  print("argv exists!")
  if result == "foo":
      # some code here, which executes when argv is "foo"
      print("Foo!")
  elif result == "bar":
      # some code here, which executes when argv is "bar"
      print("Bar")
  else:
      # some code here, which executes as the default, if the argument value is niether "foo", nor "bar"
      print("Baz")
      
# Happy coding, my homies <3
Posted by: Guest on April-20-2021

Python Answers by Framework

Browse Popular Code Answers by Language