-1

Iam trying with a simple script where i wanted to print the value that parsed via command line. But while running the script, it doesn't produce the expected output

the program name is python commadn_line_argu.py

import sys


def main():
    for arg in sys.argv[1:]:
        print "Given arfument is ", arg

if __name__=="_main_":
   main()

please see the attached screen shot for the same enter image description here

Appreciate if anyone can help on it

Thanks

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
umesh
  • 7
  • 6
  • 2
    You didn't give it any command line argument to pass in. Command line arguments are given as so: `python commadn_line_argu.py first_argument second_argument third_argument` – Davy M Dec 11 '17 at 09:24
  • You were not giving any arguments on the commandline :-) – Ivonet Dec 11 '17 at 09:24
  • I'm flagging to close this question as a simple typographical error. Reason being, others who search for a question like "Unable to pass the argument value from the console" probably are not experiencing this same error, as leaving out the arguments you are supposed to be passing is a misunderstanding of what command line arguments are rather than an actual issue with the code to pass in those values, so this question is unlikely to help others searching for an issue with this title. – Davy M Dec 11 '17 at 09:27

4 Answers4

1

give it as python filname.py argument_name

santhos
  • 46
  • 1
1

First, when executed as a script, the module's magic variable __name__ is set to "__main__" (not "_main_" - notice the double underscores), so you have to fix this test.

Then of course, if you don't pass any argument on the command line, you can't expect to have any output here...

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
-1

The __name__ value when a module is run directly is __main__, not _main_. Change the value you check against and you should be good to go. (Remember to actually pass in some arguments though!)

Dale Myers
  • 2,703
  • 3
  • 26
  • 48
-1

Your question is a duplicate of How to read/process command line arguments? You can find more details there, or use the following code:

import sys

def main():
    for arg in sys.argv[1:]:
        print "Given argument is ", arg

#_main_ should be __main__
if __name__ == "__main__":
    main()
ntg
  • 12,950
  • 7
  • 74
  • 95
Colin Schoen
  • 2,526
  • 1
  • 17
  • 26