python - 类型错误 : can only concatenate list (not "int") to list

标签 python

我正在尝试对以下代码中列表中的某些元素做一些有用的事情:

IPS=['10.254.243.83','10.254.243.82']

def commands(cmd):
    command = Popen(cmd, shell=True, stdout=PIPE)
    command_strip = command.stdout.read().strip()
    print command_strip

def main():
    for ip in IPS:
        ping = call('ping -c 3 %s' % ip, shell=True)
        commands(ping)

if __name__ == "__main__":
    main()

然后,它返回给我: python 测试.py

PING 10.254.243.83 (10.254.243.83) 56(84) bytes of data.
64 bytes from 10.254.243.83: icmp_seq=1 ttl=61 time=2.72 ms
64 bytes from 10.254.243.83: icmp_seq=2 ttl=61 time=2.05 ms
64 bytes from 10.254.243.83: icmp_seq=3 ttl=61 time=1.88 ms

--- 10.254.243.83 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 1.885/2.224/2.728/0.363 ms

Traceback (most recent call last):
  File "teste.py", line 15, in ?
    main()
  File "teste.py", line 12, in main
    commands(ping)
  File "teste.py", line 5, in commands
    command = Popen(cmd, shell=True, stdout=PIPE)
  File "/usr/lib/python2.4/subprocess.py", line 543, in __init__
    errread, errwrite)
  File "/usr/lib/python2.4/subprocess.py", line 891, in _execute_child
    args = ["/bin/sh", "-c"] + args
TypeError: can only concatenate list (not "int") to list

Can someone help me with this error ?

最佳答案

你传递的返回值

call('ping -c 3 %s' % ip, shell=True)

作为您的commands() 函数的cmd 参数。提到的返回值是一个整数,作为命令没有任何意义,因此尝试使用 Popen() 执行此整数会按预期失败。

关于python - 类型错误 : can only concatenate list (not "int") to list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5343155/

相关文章:

python - 在递归中使用 yield 平衡内存和性能

c++ - python 字符串使用特殊字符发送到 c++ dll,崩溃

python - 将文件中的单词读入集合,维护顺序

python - 如何将信息从一个 excel 复制到模板 excel?

python - 是否有与 Python matplotlib 的 tiny_layout() 等效的 MatLab?

Python 3 如何暂时禁用跟踪变量

python - music21 并行解析文件

python - python有非关系数据库吗?

python - 使用 "Flatten"或 "Reshape"在keras中获取未知输入形状的一维输出

python - 使用 ffprobe/ffmpeg 按编码顺序提取单个帧和类型