Wolf Eberhardt
Wolf Eberhardt

Reputation: 17

Python - Subprocess Output on Windows

I am trying to use the "subprocess" Module in Python to get the output from shell, the Command is "netsh wlan show interfaces". I try using "utf-8" for decoding, the "result" variable is a string, but it doesn`t work anyway.

Thanks for the help in advance!

import subprocess

class LocalNetwork:
    def networkinformations():
        result = subprocess.check_output('netsh wlan show interfaces',shell=True,encoding='utf-8').strip().splitlines()
        print(result)

LocalNetwork.networkinformations()

The traceback:

Traceback (most recent call last):
  File "d:\Python\Bibliotheken\NetyPy.py", line 8, in <module>
    LocalNetwork.networkinformations()
  File "d:\Python\Bibliotheken\NetyPy.py", line 5, in networkinformations
    result = sub.check_output('netsh wlan show interfaces', shell=True, encoding='utf-8').strip().splitlines()
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64__qbz5n2kfra8p0\Lib\subprocess.py", line 466, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64__qbz5n2kfra8p0\Lib\subprocess.py", line 550, in run
    stdout, stderr = process.communicate(input, timeout=timeout)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64__qbz5n2kfra8p0\Lib\subprocess.py", line 1196, in communicate
    stdout = self.stdout.read()
             ^^^^^^^^^^^^^^^^^^
  File "<frozen codecs>", line 322, in decode
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x84 in position 285: invalid start byte

Upvotes: -1

Views: 68

Answers (1)

Wolf Eberhardt
Wolf Eberhardt

Reputation: 17

This works:

result = subprocess.run(command, capture_output=True, text=True, check=True, encoding="cp437")

Upvotes: 1

Related Questions