So far, you aren't actually converting the program to python, you're just trying to use python as a wrapper to the shell. But you can do the grepping and awking in python too:
import subprocess
import re
lsblk = subprocess.Popen(['lsblk'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in lsblk.stdout:
if 'disk' in line:
parts = re.split(r'\s+', line.strip())
name, majmin, rm, size, ro, devtype = parts[:6]
if len(parts) > 6:
mountpoint = parts[6]
else:
mountpoint = None
print(majmin)
returncode = lsblk.wait()
if returncode:
print("things got bad. real bad.")
That's just one example. If you want a list of lines that refer to disks, you could build a list that holds lines that have 'disk' in them:
lsblk = subprocess.Popen(['lsblk'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
blockdevs = [line.strip() for line in lsblk.stdout if 'disk' in line]
returncode = lsblk.wait()
if returncode:
print("things got bad. real bad.")
print(blockdevs)