将变量传递给python中的路径/命令

时间:2013-11-21 11:11:37

标签: python variables python-2.7 subprocess

我是Python新手。我想执行一个在python中有参数的脚本。请看下面我最终想要实现的例子。

我有一个带参数的脚本:

PATH="ABCXYZ"
username = "admin"
password = "pass"
query = "select * from user;"
OUTPUT = /usr/local/<PATH>/dbscript -u username -p password -q query

如何在“/ usr / local /&lt; PATH &gt; / dbscript”中传递PATH变量?

我试过os.system&amp; subprocess,但我们不能在两者之间使用变量。 我希望OUTPUT可变数据打印yesnosome number

1 个答案:

答案 0 :(得分:0)

下面必须工作。

import subprocess as sp
PATH="ABCXYZ"
username = "admin"
password = "pass"
query = "select * from user;"
command = "/usr/local/%s/dbscript -u %s -p %s -q %s" % (PATH, username, password, query)
#Next line print '/usr/local/ABCXYZ/dbscript -u admin -p pass -q select * from user;' for me.
print command # 
sp.Popen(command.split(), shell=True)

此外,您应该使用shell参数和有用的阅读it