Python Forum
Failing to iterate over captured StdOut
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Failing to iterate over captured StdOut
#2
output.stdout is bytes. Use decode to convert to a str.
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.
tester_V likes this post
Reply


Messages In This Thread
RE: Failing to iterate over captured StdOut - by deanhystad - Jul-12-2024, 03:52 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  subprocess_run and stdout flux paul18fr 2 582 Jan-09-2025, 08:50 PM
Last Post: Gribouillis
  Failing to connect by 'net use' tester_V 1 1,097 Apr-20-2024, 06:31 AM
Last Post: tester_V
  [subprocess] Why stdout sent to stderr? Winfried 3 2,274 Jan-26-2024, 07:26 PM
Last Post: snippsat
  Performance options for sys.stdout.writelines dgrunwal 11 6,310 Aug-23-2022, 10:32 PM
Last Post: Pedroski55
  Failing regex tester_V 3 2,403 Aug-16-2022, 03:53 PM
Last Post: deanhystad
  Failing to connect to a host with WMI tester_V 6 6,213 Aug-10-2021, 06:25 PM
Last Post: tester_V
  Failing to get Stat for a Directory tester_V 11 5,582 Jul-20-2021, 10:59 PM
Last Post: Larz60+
  changing stdout and stderr Skaperen 4 4,174 Dec-02-2020, 08:58 PM
Last Post: Skaperen
  Failing to Zip files tester_V 4 3,245 Dec-01-2020, 07:28 AM
Last Post: tester_V
  Get stdout of a running process yok0 0 4,436 Aug-20-2020, 10:12 AM
Last Post: yok0

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020