FileNotFoundError: [Errno 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调用它,或者将shell=true传递到check_output中。默认情况下,subprocess程序包不会在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' ... '

相关内容

  • 没有找到相关文章

最新更新