0

Let's say, I have a python script (test.py) and it contains a function that takes a string and printed back, as following

def pri(str):
    print str

How could i call the this function (pri) from the command line? I tried:

$python test.py pri(blablalba)

but i got the following error: missing end of string

1
  • Thank you all for the answers.. all of them worked for me :) though, since i am a beginner, the answers from Andy and lithiium were so clear structured. Commented Jun 10, 2014 at 13:45

5 Answers 5

3

You'd call the python program like any command line program:

python test.py blablabla

Then inside of the program you would do this:

import sys
print sys.argv

The argv variable of sys is a list of all of the arguments passed through the command line to the program that is called. So if you wanted to call specific argument numbers you would treat it like a list. If you wanted to have a method print those arguments out you would either need to pass them to the method or have the method use that variable. In your case you would want something like this:

import sys

def pri(str):
    print str

for each in sys.argv[1:]:
    pri(each)
Sign up to request clarification or add additional context in comments.

2 Comments

If, in addition, pri should also be given on the command line, something like locals()[argv[1]](*argv[2:]) should work.
Correct, but I think they just want something simple to pass the arguments along. If that were the case I'd suggest using something like argparse to keep track of which arguments are which and limit the methods that were called based on the flags set.
2

You can call it like this (assuming your function is saved in a file named test.py)

python -c "from test import pri; pri('test')"

This will output the word test

If your file is named something other than test.py, substitute the word test in your import statement

>python -c "from test import pri; pri('test')"
test

Comments

0

You should use sys.argv. argv is a list of all the arguments passed through the command line.

try this:

import sys

def pri(s):
    print s

pri(sys.argv[1])

And you can run it doing python test.py blablabla

Comments

0

I would insist that you use the if __name__ == '__main__' construct. It allows your module to still be importable and to be usable from command line.

For your use case, it would be something like

import sys

def pri(s):
   # you stuff

# other stuff

if __name__ == '__main__':
    # test presence of params and display comprehensive errors if not applicable
    # ...
    pri(sys.argv[1])

That way python test.py blabla calls pri('blabla') but you can do import test in another python module without pri being automatically called.

Comments

0
import sys
print (sys.argv[1])

You need to use sys.argv[1]

If you save that in a script call pri.py

Use python pri.py hello from the command line and it will output hello

To print multiple words:

import sys

def pri(s):   
    print s
if __name__=="__main__":
    s = " ".join(sys.argv[1:])
    pri(s)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.