Reputation: 69
With what python code can an argument be recognized as a string?
for example:
"root:/user1/Folder1# python -W ignore run1.py Pustaka Sarawak
Govt"
In the above example, file 'run1.py' takes the argument "Pustaka Sarawak" recognizes it as a string and outputs "Govt" after performing a process. What is the code to recognize "Pustaka Sarawak" as a string? In the picture takes the Polis Diraja Malaysia and the output is 'Govt'
Upvotes: 3
Views: 3147
Reputation: 389
You can use sys library to do that.
add-arg.py
import sys
num1 = sys.argv[1]
num2 = sys.argv[2]
print("The sum is ",int(num1)+int(num2))
terminal
~$ python add-arg.py 1 2
The sum is 3
Now you might see that I have used
num1 = sys.argv[1]
Instead of
num1 = sys.argv[0]
If you put sys.argv[0] then the program name "add-arg.py' will be the argument which will be assigned to num1.
commandline-arg.py
import sys
name = sys.argv[0]
age = sys.argv[1]
print('Hi, ',name,' your age is ',age)
Terminal
~$ python commandline-arg.py sharon 22
Hi, commandline-arg.py your age is sharon
Note: The arguments given will be taken as strings
Refer here for more
Upvotes: 4
Reputation: 69
Look into python's sys.argv function.
import sys
x = str(sys.argv[1])
y = str(sys.argv[2])
x will be Pustaka, y will be Sarawak
Upvotes: 1