+ 1
Question about handling arg send at python binary
Hello everyone. I want to send to my binary's python3 in the command-line argument a string and I would like to know how can I do to change a part of the string into an int. Schema : ./binary four_4 I want to print : This is the four of 4 P.S: I don't know if I am making myself clear...
2 Answers
+ 3
the binary part might make it confusing, because in python you don't run a binary unless you explicitly converted a .py file to binary.
in python we generally say run a script.
for command line args
you can use sys.argv[ ] list sys.argv[0] is the name of the script(binary) sys.argv[1] is the first arg.
for the example above.
import sys
arg1 = sys.argv[1]
for n in arg1:
and look for digits in the string.
you might use some exception handling to make sure an argument is passed.
a better solution would be to use argparse library. read about it.
+ 1
Thank you