首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >文件"/usr/lib/python2.7/subprocess.py",第1047行,在_execute_child raise child_exception中

文件"/usr/lib/python2.7/subprocess.py",第1047行,在_execute_child raise child_exception中
EN

Stack Overflow用户
提问于 2022-01-30 07:54:42
回答 1查看 921关注 0票数 0
代码语言:javascript
复制
#! /usr/bin/env python    
import subprocess
import optparse 
def get_arguments():
    parser=optparse.OptionParser()
    parser.add_option("-i", "--interface", dest="interface", help="interface to change mac adress for")
    parser.add_option("-m", "--mac", dest="new_mac", help="change mac address")
    return parser.parse_args()
def change_mac(interface, new_mac):
    print("changing you mac address")
    subprocess.call(["ifconfig ", interface, "  down"])
    subprocess.call(["ifconfig ", interface, " hw", " ether", new_mac])
    subprocess.call(["ifconfig ", interface, " up"])
(options,arguments)=get_arguments()
change_mac(options.interface, options.new_mac)

我得到的错误如下:

代码语言:javascript
复制
Traceback (most recent call last):
  File "mac_tester.py", line 21, in <module>
    change_mac(options.interface, options.new_mac)
  File "mac_tester.py", line 15, in change_mac
    subprocess.call(["ifconfig ", interface, "  down"])
  File "/usr/lib/python2.7/subprocess.py", line 172, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.7/subprocess.py", line 394, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1047, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-30 12:46:11

在传递给subprocess.call的可执行文件的名称中有一个尾随空间,在其他参数中有一个前导空格。下面是第一行,另外两行相似:

代码语言:javascript
复制
subprocess.call(["ifconfig ", interface, "  down"])
# trailing space here ----^               ^^
# leading spaces here --------------------||

这会导致您的计算机寻找一个名为ifconfig的可执行文件,它的末尾有一个空格,但是它找不到这个可执行文件,因此您会得到一个错误。

当然,如果您收到的错误包含了错误消息中可执行文件的名称,例如No such file or directory: "ifconfig ",则可能更容易找到问题。

也许你认为你需要这些空间,因为你认为

代码语言:javascript
复制
subprocess.call(["ifconfig", interface, "down"])

最终会试图运行类似ifconfigeth0down的程序吗?情况并非如此:使用subprocess.call的原因是确保可执行文件获得您给它的确切参数,即使参数包含空格或类似的参数。

移除多余的空格,然后再试一次。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70912860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档