Python TypeError:execv()arg 2只能包含字符串

Python TypeError:execv()arg 2只能包含字符串,python,Python,我编写了以下代码,以获取Linux上当前运行接口的速度,但当我运行此脚本时,它只是抛出错误:TypeError:execv()arg 2必须只包含字符串 如有任何帮助或想法,将不胜感激 以下是脚本: 在下面的脚本中,我创建了两个函数 1) 一个是get\u Inf(),它提供了接口信息。 2) 第二个是get_intSpeed(),它从第一个函数获取接口名,并将操作系统命令ethtool传递给它,该命令通过正则表达式进一步解析,以仅获取速度,如1000Mb/s #!/grid/common/pk

我编写了以下代码,以获取Linux上当前运行接口的速度,但当我运行此脚本时,它只是抛出错误:
TypeError:execv()arg 2必须只包含字符串

如有任何帮助或想法,将不胜感激

以下是脚本:

在下面的脚本中,我创建了两个函数 1) 一个是
get\u Inf()
,它提供了接口信息。 2) 第二个是
get_intSpeed()
,它从第一个函数获取接口名,并将操作系统命令
ethtool
传递给它,该命令通过正则表达式进一步解析,以仅获取速度,如
1000Mb/s

#!/grid/common/pkgs/python/v2.7.10/bin/python
import subprocess
import netifaces
import re

''' This snippet is just to get the Speed of the running interface on the System '''



def get_Inf():
    Current_inf = netifaces.gateways()['default'][netifaces.AF_INET][1]
    return get_Inf

def get_intSpeed():
    spd = subprocess.Popen(['/sbin/ethtool', get_Inf()], stdout=subprocess.PIPE).communicate()[0]
    pat_match=re.search(".*Speed:\s+(\d+Mb/s)\s+.*", spd)       # "d" means any number of digit following the "Mb/s".
    speed = pat_match.group(1)
    return speed

def main():
    print get_intSpeed()


main()
下面是操作系统命令
/sbin/ethtool
,该命令具有 界面信息以及其他信息


我使用的是python版本2.7.10。

您的
get\u Inf()
函数将使用
return get\u Inf
语句返回自身,该语句不是字符串,这就是
execv
(由
subprocess.Popen调用)抱怨的原因。您应该从函数中返回
当前\u Inf

请显示整个错误。您发布的代码不会调用
execv
。至少,不是直接的。@BryanOakley,谢谢你的反馈,我意识到我重复了错误的值。阿尔伯特P,谢谢你的回答。虽然我意识到了这一点。
[root@tss /]# /sbin/ethtool eth0| grep Speed
              Speed: 1000Mb/s
[root@tss /]# ethtool eth0
Settings for eth0:
        Supported ports: [ TP ]
        Supported link modes:   10baseT/Half 10baseT/Full
                                100baseT/Half 100baseT/Full
                                1000baseT/Full
        Supported pause frame use: No
        Supports auto-negotiation: Yes
        Advertised link modes:  10baseT/Half 10baseT/Full
                                100baseT/Half 100baseT/Full
                                1000baseT/Full
        Advertised pause frame use: No
        Advertised auto-negotiation: Yes
        Speed: 1000Mb/s
        Duplex: Full
        Port: Twisted Pair
        PHYAD: 1
        Transceiver: internal
        Auto-negotiation: on
        MDI-X: Unknown
        Supports Wake-on: g
        Wake-on: g
        Link detected: yes