TypeError:execv()arg 2必须只包含字符串

时间:2013-12-17 01:31:19

标签: python

我在运行下面的脚本时遇到以下错误,可以帮助确定问题是什么以及如何克服它

import subprocess
import sys
import os

def main ():
    to = ''
    ssh_command = ["ssh", "-p", "29418", "review-android.quicinc.com", "gerrit",
                   "query", "--format=JSON", "--current-patch-set",
                   "--commit-message", "--files", ]

    with open('gerrit_output.txt', 'a') as fp:
        with open('caf_gerrits.txt','r') as f :
            for gerrit in f :
                print gerrit
                result = subprocess.check_output(ssh_command + [gerrit, ])
                print result
                fp.write(result)

if __name__ == '__main__':
main()

错误: -

545804

Traceback (most recent call last):
  File "test.py", line 20, in <module>

  File "test.py", line 15, in main

  File "/usr/lib/python2.7/subprocess.py", line 537, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  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

3 个答案:

答案 0 :(得分:22)

ssh_command中的第三个元素是整数。它需要是一个字符串。

e.g:

ssh_command = ["ssh", "-p", 29418, ...
#                            ^ problem

解决方案很简单,只需添加一些引号:

ssh_command = ["ssh", "-p", "29418", ...
#                           ^Now it's a string.

答案 1 :(得分:1)

首先,您需要在29418周围添加引号,如mgilson所述。第二,让我们分解你想要运行的东西:

ssh_command = ["ssh", "-p", 29418, "review-android.company.com", "gerrit",
               "query", "--format", "JSON", "--current-patch-set",
               "--commit-message", "--files", ]

等于

ssh -p 29418 review-android.company.com gerrit query --format JSON --current-patch-set --commit-message --files
(then I'm assuming you have filenames in your caf_gerrits.txt file which get appended at the end)

对我而言,有一件事是你可能想说--format=JSON在这种情况下,ssh_command数组中的元素应合并为[..., "--format=JSON", ...]。您可能想要做的另一件事是在print result行之后result=来帮助调试。

答案 2 :(得分:0)

检查您传递给 subprocess 命令的所有值。其中之一不是字符串。

我有同样的错误 TypeError: execv() arg 2 must contain only strings

p1 = Popen(['aws', 'route53', 'change-resource-record-sets', '--hosted-zone-id', zone, '--change-batch', r53json], stdout=PIPE)

但是正如你所看到的,它不是第三个参数。唯一的变量是 zone,我发现它是一个列表。传递参数 ass zone.id 修复了问题。

TypeError: execv() arg 2 must contain only strings 拥有您需要的所有线索。