正在检查 python 中的可用分区 space

Checking free partition space in python

好的,我的 raspberry pi 上有一个 chron 作业,每 15 分钟拍一张照片,现在我想用我的 piglow 板来指示分区中还剩多少 space图片存储在其中。所以我想要一个 python 脚本来检查分区上有多少 space 以及使用了多少,然后将使用的数量除以总数 space 和基于此点亮 LED。除了获取分区信息,我知道如何做所有事情。

>>> mnt = commands.getoutput("cat /proc/partitions")
>>> print mnt
major minor  #blocks  name

   3     0   19551168 hda
   3     1    6345675 hda1
   3     2   13205398 hda2
   3    64   19551168 hdb
   3    66          1 hdb2
   3    69     835348 hdb5
   3    70    9767425 hdb6
   3    71     192748 hdb7
   3    72    8755393 hdb8
  34     0   19551168 hdg
  34     1   19542568 hdg1
   8     0     992000 sda
   8     1     991747 sda1



import re
import commands

def get_size(device):
    '''
    Input  : Device name.
    Output : The devices size in 512-byte blocks.
    '''
    return commands.getoutput(
        'cat /sys/class/block/{}/size'.format(
        device))


print get_size('sda1')