David Mustea
David Mustea

Reputation: 67

Subprocess output not spliting

I trying to capture to output of a subprocess task but it won't split by '\n'.

This is the code by now:

import subprocess

a = subprocess.run('ipconfig', capture_output=True, text=True)

b = []

for line in a.stdout:
    print(line.split('\n'))

Upvotes: 0

Views: 90

Answers (1)

luuk
luuk

Reputation: 1855

a.stdout is a string; if you use a string in a for loop, you are iterating over its characters. Therefore, you are splitting each character on \n. Instead, you can simply use the following line to convert the stdout of ipconfig to a list of lines:

lines = a.stdout.split('\n')

Upvotes: 1

Related Questions