0

I wrote the following command to get the list of available WIFIs:

result = subprocess.run("netsh wlan show network",
                            shell=True, stdout=subprocess.PIPE, 
                            text=True, encoding="gbk")

This code has been run many times before and worked fine, but on this run the following error occurred:

Traceback (most recent call last):
  File "c:\Users\Administrator\Desktop\login_jxnu\auto_connect\utils.py", line 13, in get_wifi_list
    result = subprocess.run("netsh wlan show network",
  File "D:\development\anaconda\envs\deep-todo\lib\subprocess.py", line 507, in run
    stdout, stderr = process.communicate(input, timeout=timeout)
  File "D:\development\anaconda\envs\deep-todo\lib\subprocess.py", line 1121, in communicate
    stdout = self.stdout.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 3918: illegal multibyte sequence

If I change to utf-8 encoding, I get an error like this:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbd in position 3: invalid start byte

Why did this code work but suddenly failed, and is there any way to avoid this error?

I used the "chcp" command to see how the system is encoded:

res = subprocess.run("chcp", shell=True, stdout=subprocess.PIPE, text=True)

The result returned is: 活动代码页: 936\n, which corresponds to the gbk encoding.

The operating system I use is Windows 11 and the python version is 3.9.


Now, when I change gbk to utf-8, the program can run normally. I executed the following code and the output is display in comment:

res = subprocess.run("chcp", shell=True, stdout=subprocess.PIPE, text=True)
print(res.stdout)  # output:  Active code page: 65001

res = subprocess.run("chcp", shell=True, stdout=subprocess.PIPE, text=True, encoding='utf-8')
print(res.stdout)  # output:   Active code page: 65001

res = subprocess.run("chcp", shell=True, stdout=subprocess.PIPE, text=True, encoding='gbk')
print(res.stdout)  # output:  Active code page: 65001

result = subprocess.run("netsh wlan show network",
                            shell=True, stdout=subprocess.PIPE, 
                            text=True)
## error occurred
Traceback (most recent call last):
  File "c:\Users\Administrator\Desktop\login_jxnu\try.py", line 4, in <module>
    result = subprocess.run("netsh wlan show network",
  File "D:\development\anaconda\envs\deep-todo\lib\subprocess.py", line 507, in run
    stdout, stderr = process.communicate(input, timeout=timeout)
  File "D:\development\anaconda\envs\deep-todo\lib\subprocess.py", line 1121, in communicate
    stdout = self.stdout.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0xb5 in position 229: illegal multibyte sequence

# omit text=True
res = subprocess.run("chcp", shell=True, stdout=subprocess.PIPE)
print(res.stdout)  # output:  b'Active code page: 65001\r\n'
  • Probably the command now returns different output than before when there was no error. – mkrieger1 Sep 02 '23 at 10:02
  • Please [edit] your question to improve your [mcve]. In particular, share what if you add `encoding="gbk"` and/or `encoding="utf-8"` to `subprocess.run("chcp", …`, and what if you omit `encoding="gbk"` in failing command? – JosefZ Sep 02 '23 at 10:25
  • I updated the question according to your suggestion – idea steps Sep 02 '23 at 10:55
  • @mkrieger1 There are always Chinese and English in the obtained output before error occurred – idea steps Sep 02 '23 at 11:07
  • Please omit `text=True` and share `result.stdout` (sanitize addresses). – JosefZ Sep 02 '23 at 11:15
  • When the following code is executed: ` when execute `res = subprocess.run("chcp", shell=True, stdout=subprocess.PIPE) print(res.stdout) `, It displays the result `b'Active code page: 65001\r\n'` – idea steps Sep 02 '23 at 11:44
  • Well done however I thought `result = subprocess.run("netsh wlan show network", shell=True, stdout=subprocess.PIPE)` – JosefZ Sep 02 '23 at 13:50

0 Answers0