我正在用Python构建一个应用程序插件,它允许用户使用简单的脚本任意扩展应用程序(在Mac OS X下工作)。执行 Python 脚本很容易,但有些用户更喜欢使用 Ruby 等语言。
根据我所读到的内容,我可以使用 subprocess
轻松执行 Ruby 脚本(或其他任意 shell 脚本),并使用管道捕获其输出;这不是问题,网上有很多例子。但是,我需要为脚本提供多个变量(例如一段文本以及有关脚本正在修改的文本的一些简单 bool 信息),并且我无法找出执行此操作的最佳方法。
有人对实现这一目标的最佳方法有建议吗?我的目标是为脚本提供所需的信息,并使用访问脚本中的信息所需的最少代码。
提前致谢!
最佳答案
参见http://docs.python.org/library/subprocess.html#using-the-subprocess-module
args should be a string, or a sequence of program arguments. The program to execute is normally the first item in the args sequence or the string if a string is given, but can be explicitly set by using the executable argument.
因此,您的调用可以如下所示
p = subprocess.Popen( args=["script.sh", "-p", p_opt, "-v", v_opt, arg1, arg2] )
您已将任意 Python 值放入 subprocess.Popen
的参数中。
关于python - 如何通过Python执行任意shell脚本并传递多个变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/714360/