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
3 Answers 3
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 )
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.
Comments
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
sys.argvlist and choose the right action. Checkargparsethe module<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.sys.argvis -- usually -- a poor choice. Notice that the answers all specifically recommended avoiding this.