繁体   English   中英

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

[英]FileNotFoundError: [Errno 2] No such file or directory: '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'

您需要通过 shell 使用cmd.exe /c或将shell=true传递到check_output来调用它。 subprocess进程package在Windows默认不运行在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", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.check_output(["ls"])
b' ... '

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM