execute python script with function from command line, Linux
Solution 1
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 )
Solution 2
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.
Solution 3
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
knittledan
Updated on July 05, 2022Comments
-
knittledan almost 2 years
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
-
Thomas over 12 yearsGood combination of briefly answering X by mostly addressing Y
-
Nick Perkins over 12 yearsdoes this require a hashbang?
-
Helk over 6 years@S.Lott What is the purpose of the function_map dict here? Why does it have two keys for the command?
-
Timo about 2 yearsIf I have several functions, I need to have several function_maps?
-
Timo about 2 yearsThis answer shows valid code but does not answer the question. OP asks how to call the function from cmd. Your example calls the argument from cmd.