Python 类型错误:execv() arg 2 必须只包含字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20624342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:51:27  来源:igfitidea点击:

TypeError: execv() arg 2 must contain only strings

python

提问by user2955256

I am getting the following error when running the script below,can anyhelp to identify what the problem is and how to overcome it

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

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()

ERROR:-

错误:-

545804

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

回答by mgilson

The third element in ssh_commandis an integer. It needs to be a string.

in 的第三个元素ssh_command是一个整数。它必须是一个字符串。

e.g:

例如:

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

And the solution is simple, just add some quotes:

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

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

回答by misakm

First you need to add the quotes around 29418as mgilson mentioned. Second let's break down what you're trying to run:

首先,您需要29418像 mgilson 提到的那样添加引号。其次,让我们分解您要运行的内容:

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

That equals

那等于

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)

One thing that pops out at me is that you may want to say --format=JSONin which case your elements in the ssh_commandarray should be combined as [..., "--format=JSON", ...]. The other thing you may want to do is print resultafter your result=line to help with the debugging.

我突然想到的一件事是您可能想说--format=JSON在这种情况下ssh_command数组中的元素应该组合为[..., "--format=JSON", ...]. 您可能想要做的另一件事是print result在您的result=线路之后帮助调试。