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()
print(labels)

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

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

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

print("-- finished --");




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

No comments: