我正在尝试使用Subprocess从Python调用shell脚本。当我从终端调用我的脚本时,我使用:
// Passing string, int, string, string
sh script.sh "firstArgument" 4040 "thirdArgument" "fourthArgument"
所以,我认为这是使用subprocess
在Python中执行此操作的正确方法:
args = ['sh script.sh', "firstArgument", 3030, "thirdArgument", "fourthArgument"]
val = subprocess.check_call(args, shell=True)
但是当我运行此代码时,我收到以下错误:
Traceback (most recent call last):
File "main_console.py", line 129, in <module>
app.main()
File "main_console.py", line 34, in main
val = subprocess.check_call(args, shell=True)
File "/usr/lib/python2.7/subprocess.py", line 506, in check_call
retcode = call(*popenargs, **kwargs)
File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
TypeError: execv() arg 2 must contain only strings
如何在不收到错误的情况下向我的脚本发送这些参数?
问题不在于论证本身。我可以向我的脚本发送一个整数。现在的问题是:当我从终端运行我的sh时,就像这样:
sh script.sh "blabla" "400" "blabla" "blabla"
这很有效。但是当我用这段代码运行我的python文件时:
args = ['sh script.sh', "firstArgument", 3030, "thirdArgument", "fourthArgument"]
val = subprocess.check_call(args, shell=True)
我收到以下错误:
Removing proxy configuration.
(gconftool-2:28078): GConf-WARNING **: Failed to load source "xml:readwrite:/home/root/.gconf": Failed: Could not make directory `/home/root/.gconf': No such file or directory
**
GConf:ERROR:gconftool.c:969:main: assertion failed: (err == NULL)
Aborted
(gconftool-2:28079): GConf-WARNING **: Failed to load source "xml:readwrite:/home/root/.gconf": Failed: Could not make directory `/home/root/.gconf': No such file or directory
**
GConf:ERROR:gconftool.c:969:main: assertion failed: (err == NULL)
Aborted
(gconftool-2:28080): GConf-WARNING **: Failed to load source "xml:readwrite:/home/root/.gconf": Failed: Could not make directory `/home/root/.gconf': No such file or directory
**
GConf:ERROR:gconftool.c:969:main: assertion failed: (err == NULL)
Aborted
(gconftool-2:28081): GConf-WARNING **: Failed to load source "xml:readwrite:/home/root/.gconf": Failed: Could not make directory `/home/root/.gconf': No such file or directory
**
GConf:ERROR:gconftool.c:969:main: assertion failed: (err == NULL)
Aborted
(gconftool-2:28082): GConf-WARNING **: Failed to load source "xml:readwrite:/home/root/.gconf": Failed: Could not make directory `/home/root/.gconf': No such file or directory
**
GConf:ERROR:gconftool.c:969:main: assertion failed: (err == NULL)
Aborted
(gconftool-2:28083): GConf-WARNING **: Failed to load source "xml:readwrite:/home/root/.gconf": Failed: Could not make directory `/home/root/.gconf': No such file or directory
**
GConf:ERROR:gconftool.c:969:main: assertion failed: (err == NULL)
Aborted
(gconftool-2:28084): GConf-WARNING **: Failed to load source "xml:readwrite:/home/root/.gconf": Failed: Could not make directory `/home/root/.gconf': No such file or directory
**
GConf:ERROR:gconftool.c:969:main: assertion failed: (err == NULL)
Aborted
(gconftool-2:28085): GConf-WARNING **: Failed to load source "xml:readwrite:/home/root/.gconf": Failed: Could not make directory `/home/root/.gconf': No such file or directory
**
GConf:ERROR:gconftool.c:969:main: assertion failed: (err == NULL)
Aborted
我的代码有问题吗?
答案 0 :(得分:1)
check_call(['sh', 'script.sh', "firstArgument", '3030', "thirdArgument", "fourthArgument"])
如果数字在变量中,则需要输入一个字符串:
portNum = 3030
check_call(['sh', 'script.sh', "firstArgument", str(portNum), "thirdArgument", "fourthArgument"])
并且不使用shell=True
。即使您正在调用一个shell,shell=True
只应在您想要由shell解释参数列表本身时使用。它的使用几乎普遍是错误的东西。