0

I want to run df command on the specific directory from where the python file will be executed and format the output in dictionary format as: {'Mounted on': 'Available'}

import os
stream = os.popen('df -k')
output = stream.read()
output

When I run the code the output comes in the following format:

Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/mapper/lg_root
               24489260  2533832  20693860  11% /
devtmpfs         2021940       0   2021940   0% /dev
tmpfs            2022896       0   2022896   0% /dev/shm
tmpfs            2022896    1544   2021352   1% /run
/dev/mapper/logs
                 2022896       0   2022896   0% /var/log
tmpfs            2022896       0   2022896   0% /tmp

The pattern of output in the rows is different. In 2 and 7 rows, the output is different from other rows. I tried split('\n'), strip(' ') but the output was inconsistent and incorrect. How to format the output in the expected way?

gd1
  • 655
  • 1
  • 10
  • 21
  • Have you seen this answer [Store output diskspace df -h JSON](https://stackoverflow.com/questions/35211716/store-output-diskspace-df-h-json) ? – hurlenko Mar 31 '20 at 16:08
  • @hurlenko Thank you for pointing me to the right place. It helped. – gd1 Mar 31 '20 at 16:46

1 Answers1

0

In this case, only the formatting of the output was the issue. So the solution is:

import os
stream = os.popen('df -P -k')
output = stream.read()
output

-P, --portability: use the POSIX output format

Manual: df manual

gd1
  • 655
  • 1
  • 10
  • 21