Is this possible in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is this possible in python?

If you have idea about linux bash, we can provide arguments in the same line in the terminal. For Example in sed, the format is, sed OPTIONS... [SCRIPT] [INPUTFILE...] Let's say, to greet 'Hello' there is a python program, greet.py Input in cmd (Suppose, my python file is reg. in env. path) ========== greet Arun Output ====== Hello Arun Wondering, whether this is possible in python, any idea how to?

17th Apr 2021, 9:54 AM
Arun Bhattacharya
Arun Bhattacharya - avatar
4 Answers
+ 4
Yes you can do it in python. You just need to import sys library. import sys your_arguments = sys.argv # all the arguments are stored in this list print(f"Hello {your_arguments[1]} ") # first element is your python file name
17th Apr 2021, 10:02 AM
Hoh Shen Yien
Hoh Shen Yien - avatar
+ 1
Yes, you'd have to add the directory with the code in it to your $PATH. That way, you can call it without './' Then make the python code executable chmod 755 <filename> Then in the python code, make sure you have code for argc & argv. Say the code is called greet.py: someone@linux:$ greet.py Arun EDIT: Nice Arun Bhattacharya . Just so you know, Windows also has a PATH variable you can add a folder to. It will then run any code with the correct shebang at the top when called. It's treated as a command instead of you having to phisically type python3 <code path> *<args>
17th Apr 2021, 10:04 AM
Slick
Slick - avatar
+ 1
Thanks Hoh Shen Yien, I think you meant, print(f"Hello {your_arguments[1]}") Thanks Slick, currently am on Windows, but surely it helped me understanding it in Linux.
17th Apr 2021, 10:14 AM
Arun Bhattacharya
Arun Bhattacharya - avatar
0
Oh yes forgotten to add the curly bracket
17th Apr 2021, 10:15 AM
Hoh Shen Yien
Hoh Shen Yien - avatar