Sunday, December 1, 2013

Read file system information with Python

Last post show how to "Display file system information with Linux df command". Enter the command "df -h /" in terminal will display the result like this, in two lines:

We can read the lines and display in Python:

Example Python code:
import os

df = os.popen("df -h /")

#The first line content the label
labels = df.readline()

#we look for the 2nd line only
the2ndLine = df.readline()

labelsArray = labels.split()[1:6]
dfArray = the2ndLine.split()[1:6]

for index in range(len(labelsArray)):
    print index, " - ", labelsArray[index], " : ", dfArray[index]

print("-- finished --");

- Similar in run host command and get result with popen() in C language.

No comments: