Python subprocess.check_output()

时间:2018-11-15 07:09:25

标签: subprocess

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

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

-错误消息-

   Traceback (most recent call last):
      File "test.py", line 52, in 
        mystring = subprocess.check_output(["sudo iwlist wlan0 scan"], universal_newlines=True)
      File "/usr/lib/python2.7/subprocess.py", line 212, in check_output
        process = Popen(stdout=PIPE, *popenargs, **kwargs)
      File "/usr/lib/python2.7/subprocess.py", line 390, in __init__
        errread, errwrite)
      File "/usr/lib/python2.7/subprocess.py", line 1024, in _execute_child
        raise child_exception
    OSError: [Errno 2] No such file or directory

出什么问题了?

2 个答案:

答案 0 :(得分:1)

  

出什么问题了?

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

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

["sudo iwlist wlan0 scan"]

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

答案 1 :(得分:0)

您的代码似乎正在使用位于以下位置的名为“ subprocess”的文件:/usr/lib/python2.7/subprocess.py

但是文件或目录不存在。您可以更改目录或将文件放在正确的文件夹中。

如果是打包的。您是否使用pip或sth安装了软件包?它是进口的吗?