0

我正在尝试使用Subprocess从 Python 调用 shell 脚本。当我从终端调用我的脚本时,我使用:

// Passing string, int, string, string
sh script.sh "firstArgument" 4040 "thirdArgument" "fourthArgument"

所以,我认为这是在 Python 中使用以下方法的正确方法subprocess

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

我的代码有问题吗?

4

1 回答 1

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=True也应该仅在您希望参数列表本身由外壳程序解释时使用。它的使用几乎普遍是错误的。

于 2013-10-20T23:07:47.547 回答