首页 > 解决方案 > Python subprocess.check_output()

问题描述

mystring = subprocess.check_output(["sudo iwlist wlan0 scan"], universal_newlines=True)
word = 'Devsign2G'
print (mystring)
print (word)

if word in str(mystring):
    print ('success')

-错误信息-

   回溯(最近一次通话最后):
      文件“test.py”,第 52 行,在
        mystring = subprocess.check_output(["sudo iwlist wlan0 scan"], universal_newlines=True)
      文件“/usr/lib/python2.7/subprocess.py”,第 212 行,在 check_output 中
        进程 = Popen(stdout=PIPE, *popenargs, **kwargs)
      文件“/usr/lib/python2.7/subprocess.py”,第 390 行,在 __init__
        读错,写错)
      _execute_child 中的文件“/usr/lib/python2.7/subprocess.py”,第 1024 行
        引发 child_exception
    OSError: [Errno 2] 没有这样的文件或目录

问题是什么?

标签: subprocess

解决方案


问题是什么?

指的OSError: [Errno 2] No such file or directory是要执行的子进程命令。

程序参数必须按顺序单独传递,因此请更改

["sudo iwlist wlan0 scan"]

["sudo", "iwlist", "wlan0", "scan"]

推荐阅读