Jul-12-2024, 03:52 AM
(This post was last modified: Jul-12-2024, 03:52 AM by deanhystad.)
output.stdout is bytes. Use decode to convert to a str.
The reason things work when you write output to a file is that reading from a text file invokes a decoder to convert the bytes in the file to python strings.
import subprocess output = subprocess.run( ["arp", "-a"], capture_output=True, creationflags=subprocess.CREATE_NO_WINDOW, ) for i, line in enumerate(output.stdout.decode('utf-8').splitlines()): print(i, line)The "encoding" argument in the run() command is for input, not output. Your command doesn't use input, so there is no need to provide an encoder.
The reason things work when you write output to a file is that reading from a text file invokes a decoder to convert the bytes in the file to python strings.