FileNotFoundError:[错误2]没有这样的文件或目录:'netsh'

问题描述

这是我的python代码,用于查看连接到我的WIFI的设备:

  import subprocess

  meta_data = subprocess.check_output(['netsh','wlan','show','profiles'])
  data = meta_data.decode('utf-8',errors="backslashreplace")
  data = data.split('\n')
  names = []

  for i in data:
      if "All Users Profiles" in i:
          i = i.split(":")
          i = i[1]
          i = i[1:-1]
          names.append(i)
  print("Systems Connected To Your WIFI ARE ")
  print()
  for name in names:
      print(name)

这是我运行代码后不断出现的错误:

   FileNotFoundError: [Errno 2] No such file or directory: 'netsh'

解决方法

您需要使用cmd.exe /c通过外壳或通过将shell=true传递到check_output来调用它。在Windows上,默认情况下subprocess程序包不在shell中运行。

Python 3.8.1 (tags/v3.8.1:1b293b6,Dec 18 2019,23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help","copyright","credits" or "license" for more information.
>>> import subprocess
>>> subprocess.check_output(["dir"])
Traceback (most recent call last):
  File "<stdin>",line 1,in <module>
  ...
FileNotFoundError: [WinError 2] The system cannot find the file specified
>>> subprocess.check_output(["cmd.exe","/c","dir"])
b' ... '
>>> subprocess.check_output(["dir"],shell=True)
b' ... '

这只是您在Windows上需要做的事情。

Python 3.8.0 (default,Oct 28 2019,16:14:01)
[GCC 8.3.0] on linux
Type "help","credits" or "license" for more information.
>>> import subprocess
>>> subprocess.check_output(["ls"])
b' ... '

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...