H
H
HiElias2021-03-15 20:38:25
linux
HiElias, 2021-03-15 20:38:25

How to properly execute lsblk command via python on Linux?

Good afternoon!
I'm trying to execute the lsblk linux command and output them (stdout). Everything is output except for the FSTYPE column. I do not understand why the data from this column is not displayed.

import sys
import subprocess

f = open('text.txt', 'r')



stdout_file = sys.stdout
stdout_file.write(f.read())

x = 'nvme0n1'

Y = subprocess.run(['lsblk', '-o', 'NAME,FSTYPE,SIZE,TYPE,MOUNTPOINT', '-l'], stdout=subprocess.PIPE, encoding='utf-8').stdout
print(Y)
print(type(Y))


Conclusion:
NAME      FSTYPE   SIZE TYPE MOUNTPOINT
nvme0n1          476,9G disk 
nvme0n1p1          512M part 
nvme0n1p2        476,4G part /run/host/share/icons

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Romses Panagiotis, 2021-03-15
@HiElias

I don't know why, but isn't it easier to run with a key -Jto get JSON?!
lsblk -o NAME,FSTYPE,SIZE,TYPE,MOUNTPOINT -l -J

{
   "blockdevices": [
      {"name":"loop0", "fstype":"squashfs", "size":"9.1M", "type":"loop", "mountpoint":"/snap/canonical-livepatch/95"},
      {"name":"loop1", "fstype":"squashfs", "size":"91.3M", "type":"loop", "mountpoint":"/snap/go/7165"},
      {"name":"loop3", "fstype":"squashfs", "size":"99.2M", "type":"loop", "mountpoint":"/snap/core/10859"},
...

Added
Some partitions are valid with fstype null. I think they should just be skipped.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question