This is my python hello.py
script:
def hello(a,b): print "hello and that's your sum:" sum=a+b print sum import sys if __name__ == "__main__": hello(sys.argv[2])
The problem is that it can't be run from the windows command line prompt, I used this command:
C:\Python27>hello 1 1
But it didn't work unfortunately, may somebody please help?
You can use the command line arguments by using the sys. argv[] array. The first index of the array consists of the python script file name. And from the second position, you'll have the command line arguments passed while running the python script.
You can use wt.exe to open a new instance of Windows Terminal from the command line. You can also use the execution alias wt instead. If you built Windows Terminal from the source code on GitHub, you can open that build using wtd.exe or wtd .
import sys
out of hello function.'
should be escaped or should be surrouned by "
.python hello.py <some-number> <some-number>
in command line?import sys def hello(a,b): print "hello and that's your sum:", a + b if __name__ == "__main__": a = int(sys.argv[1]) b = int(sys.argv[2]) hello(a, b)
I found this thread looking for information about dealing with parameters; this easy guide was so cool:
import argparse parser = argparse.ArgumentParser(description='Script so useful.') parser.add_argument("--opt1", type=int, default=1) parser.add_argument("--opt2") args = parser.parse_args() opt1_value = args.opt1 opt2_value = args.opt2
run like:
python myScript.py --opt2 = 'hi'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With