up vote 2 down vote favorite
1
share [g+] share [fb]

I have my python file called convertImage.py and inside the file I have a script that converts an image to my liking, the entire converting script is set inside a function called convertFile(fileName)

Now my problem is I need to execute this python script from the linux command line while passing the convertFile(fileName) function along with it.

example:

 linux user$: python convertImage.py convertFile(fileName)

This should execute the python script passing the appropriate function.

example:

def convertFile(fileName):

    import os, sys
    import Image
    import string

    splitName = string.split(fileName, "_")
    endName = splitName[2]
    splitTwo = string.split(endName, ".")
    userFolder = splitTwo[0]

    imageFile = "/var/www/uploads/tmp/"+fileName

    ...rest of the script...

    return

What is the right way to execute this python script and properly pass the file name to the function from the liunx command line?

Thank in advanced

link|improve this question

2  
there's no such thing. Parse the sys.argv list and choose the right action. Check argparse the module – JBernardo Oct 7 '11 at 18:53
As mentioned by S.Lott, <program> <subcommand> <command arguments> is a very common style. It's best for the user to have command names, not knowledge of the internal implementation. And using parentheses (which require escaping in bash) as a required part of your syntax is just mean. – Jefromi Oct 7 '11 at 19:19
The parentheses were only for example reasons but I appreciated all the help and got it to work using sys.argv – knittledan Oct 7 '11 at 20:35
@knittledan: sys.argv is -- usually -- a poor choice. Notice that the answers all specifically recommended avoiding this. – S.Lott Oct 7 '11 at 21:41
feedback

3 Answers

up vote 8 down vote accepted

This

if __name__ == "__main__":
    command= " ".join( sys.argv[1:] )
    eval( command )

This will work. But it's insanely dangerous.

You really need to think about what your command-line syntax is. And you need to think about why you're breaking the long-established Linux standards for specifying arguments to a program.

For example, you should consider removing the useless ()'s in your example. Make it this, instead.

python convertImage.py convertFile fileName

Then, you can -- with little work -- use argparse to get the command ("convertFile") and the arguments ("fileName") and work within the standard Linux command line syntax.

function_map = { 
    'convertFile': convertFile,
    'conv': convertFile,
}
parser = argparse.ArgumentParser()
parser.add_argument( 'command', nargs=1 )
parser.add_argument( 'fileName', nargs='+' )
args= parser.parse_args()
function = function_map[args.command]
function( args.fileName )
link|improve this answer
1  
Good combination of briefly answering X by mostly addressing Y – Thomas Oct 7 '11 at 19:03
feedback

Quick and dirty way:

linux user$: python convertImage.py convertFile fileName

and then in convertImage.py

if __name__ == '__main__':
    import sys
    function = getattr(sys.modules[__name__], sys.argv[1])
    filename = sys.argv[2]
    function(filename)

A more sophisticated approach would use argparse (for 2.7 or 3.2+) or optparse.

link|improve this answer
argparse is also in 2.7 – Petr Viktorin Oct 7 '11 at 18:59
@PetrViktorin: Thanks, updated. – Ethan Furman Oct 7 '11 at 19:01
feedback

Create a top-level executable part of your script that parses command-line argument(s) and then pass it to your function in a call, like so:

import os, sys
#import Image
import string


def convertFile(fileName):
    splitName = string.split(fileName, "_")
    endName = splitName[2]
    splitTwo = string.split(endName, ".")
    userFolder = splitTwo[0]

    imageFile = "/var/www/uploads/tmp/"+fileName

    print imageFile     # (rest of the script)

    return


if __name__ == '__main__':
    filename = sys.argv[1]
    convertFile(filename)

Then, from a shell,

$ convertImage.py the_image_file.png
/var/www/uploads/tmp/the_image_file.png
link|improve this answer
-1 for not suggesting using argparse – Kimvais Oct 7 '11 at 19:01
does this require a hashbang? – Nick Perkins Oct 12 '11 at 20:38
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.