pyinstaller error: OSError: [WinError 6] The handle is invalid
pyinstaller error: OSError: [WinError 6] The handle is invalid
此文件使用终端命令获取 wifi 密码 netsh wlan show profiles
我之前用pyinstaller创建了一些.exe,它们运行得很好。
代码:
import subprocess
import time
import sys
import re
command_output = subprocess.run(["netsh", "wlan", "show", "profiles"], capture_output = True).stdout.decode()
profile_names = (re.findall("All User Profile : (.*)\r", command_output))
wifi_list = []
if len(profile_names) != 0:
for name in profile_names:
wifi_profile = {}
profile_info = subprocess.run(["netsh", "wlan", "show", "profile", name], capture_output = True).stdout.decode()
if re.search("Security key : Absent", profile_info):
continue
else:
wifi_profile["ssid"] = name
profile_info_pass = subprocess.run(["netsh", "wlan", "show", "profile", name, "key=clear"], capture_output = True).stdout.decode()
password = re.search("Key Content : (.*)\r", profile_info_pass)
if password == None:
wifi_profile["password"] = None
else:
wifi_profile["password"] = password[1]
wifi_list.append(wifi_profile)
for x in range(len(wifi_list)):
print(wifi_list[x])
time.sleep(5)
print("No more WiFi Profiles Found")
time.sleep(3)
sys.exit()
这是我在 运行 .exe:
时得到的错误
Traceback (most recent call last):
File "GetWiFiPassWord.py", line 6, in <module>
File "subprocess.py", line 453, in run
File "subprocess.py", line 709, in __init__
File "subprocess.py", line 1006, in _get_handles
OSError: [WinError 6] The handle is invalid
这个错误显然是抛出的,因为:
Line 1117 in subprocess.py is: p2cread = _winapi.GetStdHandle(_winapi.STD_INPUT_HANDLE)
The service processes do not have a STDIN associated with them (TBC).
可以通过提供文件或空设备作为 popen
的标准输入参数来避免此问题。
In Python 3.x, you can simply pass stdin=subprocess.DEVNULL
.
E.g.
subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL)
In Python 2.x, you need to get a filehandler to null, then pass
that to popen:
devnull = open(os.devnull, 'wb')
subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=devnull)
参考:
你的问题:
subprocess.run(["netsh", "wlan", "show", "profiles"], capture_output = True, stdin=subprocess.DEVNULL).stdout.decode()
我在使用 pyinstaller 时遇到了同样的问题:
PyInstaller: 4.5.1
Python: 3.9.6
Platform: Windows-10-10.0.19042-SP0
感谢Behdad Abdollahi Moghadam。我通过将 stdin
和 stder
添加到 subprocess.check_output
解决了我的问题
subprocess.check_output("C:\Program Files (x86)\xxx.exe", stdin=subprocess.DEVNULL, stderr=subprocess.STDOUT)
此文件使用终端命令获取 wifi 密码 netsh wlan show profiles
我之前用pyinstaller创建了一些.exe,它们运行得很好。
代码:
import subprocess
import time
import sys
import re
command_output = subprocess.run(["netsh", "wlan", "show", "profiles"], capture_output = True).stdout.decode()
profile_names = (re.findall("All User Profile : (.*)\r", command_output))
wifi_list = []
if len(profile_names) != 0:
for name in profile_names:
wifi_profile = {}
profile_info = subprocess.run(["netsh", "wlan", "show", "profile", name], capture_output = True).stdout.decode()
if re.search("Security key : Absent", profile_info):
continue
else:
wifi_profile["ssid"] = name
profile_info_pass = subprocess.run(["netsh", "wlan", "show", "profile", name, "key=clear"], capture_output = True).stdout.decode()
password = re.search("Key Content : (.*)\r", profile_info_pass)
if password == None:
wifi_profile["password"] = None
else:
wifi_profile["password"] = password[1]
wifi_list.append(wifi_profile)
for x in range(len(wifi_list)):
print(wifi_list[x])
time.sleep(5)
print("No more WiFi Profiles Found")
time.sleep(3)
sys.exit()
这是我在 运行 .exe:
时得到的错误Traceback (most recent call last):
File "GetWiFiPassWord.py", line 6, in <module>
File "subprocess.py", line 453, in run
File "subprocess.py", line 709, in __init__
File "subprocess.py", line 1006, in _get_handles
OSError: [WinError 6] The handle is invalid
这个错误显然是抛出的,因为:
Line 1117 in subprocess.py is:
p2cread = _winapi.GetStdHandle(_winapi.STD_INPUT_HANDLE)
The service processes do not have a STDIN associated with them (TBC).
可以通过提供文件或空设备作为 popen
的标准输入参数来避免此问题。
In Python 3.x, you can simply pass
stdin=subprocess.DEVNULL
. E.g.subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL)
In Python 2.x, you need to get a filehandler to null, then pass that to popen:
devnull = open(os.devnull, 'wb') subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=devnull)
参考:
你的问题:
subprocess.run(["netsh", "wlan", "show", "profiles"], capture_output = True, stdin=subprocess.DEVNULL).stdout.decode()
我在使用 pyinstaller 时遇到了同样的问题:
PyInstaller: 4.5.1
Python: 3.9.6
Platform: Windows-10-10.0.19042-SP0
感谢Behdad Abdollahi Moghadam。我通过将 stdin
和 stder
添加到 subprocess.check_output
subprocess.check_output("C:\Program Files (x86)\xxx.exe", stdin=subprocess.DEVNULL, stderr=subprocess.STDOUT)