1,python
#!/usr/bin/python
import os
import statvfs
vfs=os.statvfs("/home")
#print vfs
#print dir(os.statvfs)
#print dir(statvfs)
#availsize
avl_size=vfs[statvfs.F_BAVAIL]*vfs[statvfs.F_BSIZE]/(1024*1024*1024)
#totalsize
tol_size=vfs[statvfs.F_BLOCKS]*vfs[statvfs.F_BSIZE]/(1024*1024*1024)
#percentsize
percent=round((tol_size-avl_size)*1.0/tol_size*100,3)
#or from __future__ import division round((tol_size*avl_size)/tol_size*100,3)
print 'THE HOME DIRS USED_PERCENT IS %s%%'%percent
然后执行chmod a+x xxx.py
python help
命令行中:import os ;import statvfs
然后查看帮帮助:help(os),help(os.statvfs),help(statvfs)
2,C语言
#include <stdio.h>
#include <sys/statvfs.h>
int main()
{
struct statvfs diskInfo;
statvfs("/home/", &diskInfo);
unsigned long long blocksize = diskInfo.f_bsize;
unsigned long long totalsize = blocksize * diskInfo.f_blocks;
printf("Total_size = %llu B = %llu KB = %llu MB = %llu GB\n",
totalsize, totalsize>>10, totalsize>>20, totalsize>>30); //64bit print out
unsigned long long freeDisk = diskInfo.f_bfree * blocksize;
unsigned long long availableDisk = diskInfo.f_bavail * blocksize;
printf("Disk_free = %llu MB = %llu GB\nDisk_available = %llu MB = %llu GB\n",
freeDisk>>20, freeDisk>>30, availableDisk>>20, availableDisk>>30);
return 0;
}
然后执行:gcc xxx.c -o diskinfo
可以使用statfs函数,不懂使用man statfs
从上可以看到,函数名称在python中和c中类似,其实有些东西是通用的