使用来自Linux的命令行函数执行python脚本

时间:2022-12-31 01:12:54

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)

我有一个叫convertImage的python文件。在这个文件中,我有一个脚本可以将图像转换成我喜欢的格式,整个转换脚本都设置在一个名为convertFile(文件名)的函数中

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.

现在,我的问题是,在传递convertFile(文件名)函数时,我需要从linux命令行执行这个python脚本。

example:

例子:

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

This should execute the python script passing the appropriate function.

这将执行传递适当函数的python脚本。

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?

执行这个python脚本并从liunx命令行正确地将文件名传递给函数的正确方法是什么?

Thank in advanced

感谢在发达

3 个解决方案

#1


22  

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.

您确实需要考虑您的命令行语法是什么。您还需要考虑为什么要打破长期建立的Linux标准来为程序指定参数。

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.

然后,您可以使用argparse来获取命令(“convertFile”)和参数(“fileName”),并使用标准的Linux命令行语法。

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 )

#2


4  

Quick and dirty way:

快速和肮脏的方式:

linux user$: python convertImage.py convertFile fileName

and then in convertImage.py

然后在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.

更复杂的方法是使用argparse(2.7或3.2+)或optparse。

#3


0  

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

#1


22  

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.

您确实需要考虑您的命令行语法是什么。您还需要考虑为什么要打破长期建立的Linux标准来为程序指定参数。

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.

然后,您可以使用argparse来获取命令(“convertFile”)和参数(“fileName”),并使用标准的Linux命令行语法。

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 )

#2


4  

Quick and dirty way:

快速和肮脏的方式:

linux user$: python convertImage.py convertFile fileName

and then in convertImage.py

然后在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.

更复杂的方法是使用argparse(2.7或3.2+)或optparse。

#3


0  

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