python子进程模块调用方法

问题描述 投票:0回答:1

我在单个脚本中执行多个命令时,在call模块中使用subprocess方法时遇到问题。以下代码可以正常工作:

subprocess.call('ifconfig eth0 down', shell=True)
subprocess.call('ifconfig eth0 hw ether 00:99:99:99:99:99', shell=True)
subprocess.call('ifconfig eth0 up', shell=True)

但是从某些来源,我发现下面的代码据说可以正常工作,但不能与我一起工作:

subprocess.call(['ifconfig eth0 down'])
subprocess.call(['ifconfig eth0 hw ether 00:99:99:99:99:99'])
subprocess.call(['ifconfig eth0 up'])

就我而言,它给出了以下错误:

Traceback (most recent call last):
  File "new.py", line 3, in <module>
    subprocess.call(['ifconfig eth0 down'])
  File "/home/anaconda3/lib/python3.7/subprocess.py", line 339, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/home/anaconda3/lib/python3.7/subprocess.py", line 800, in __init__
    restore_signals, start_new_session)
  File "/home/anaconda3/lib/python3.7/subprocess.py", line 1551, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ifconfig eth0 down': 'ifconfig eth0 down'

任何人都不知道可能是什么问题。预先感谢。

python subprocess
1个回答
0
投票

您没有链接到“某些来源”,但我怀疑您有看错的地方。这些例子可能看起来像这样:

subprocess.call(['ifconfig', 'eth0', 'down'])

即,如果您未设置shell=True,则subprocess.call的第一个参数必须是命令行参数的列表。

© www.soinside.com 2019 - 2024. All rights reserved.