获取硬盘使用率

def disk_stat():

    import os

    hd={}

    disk = os.statvfs("/")

    hd['available'] = disk.f_bsize * disk.f_bavail

    hd['capacity'] = disk.f_bsize * disk.f_blocks

    hd['used'] = disk.f_bsize * disk.f_bfree

    print float(hd['used']) / float(hd['capacity'])

    return hd

转载于:https://my.oschina.net/zfschool/blog/788334

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java中的File类和FileSystem类来获取磁盘使用率。 方法一:使用File类 ```java import java.io.File; public class DiskSpace { public static void main(String[] args) { File file = new File("/"); long totalSpace = file.getTotalSpace(); long freeSpace = file.getFreeSpace(); long usedSpace = totalSpace - freeSpace; System.out.println("Total space: " + totalSpace / (1024 * 1024) + "MB"); System.out.println("Free space: " + freeSpace / (1024 * 1024) + "MB"); System.out.println("Used space: " + usedSpace / (1024 * 1024) + "MB"); double usage = (double) usedSpace / totalSpace * 100; System.out.println("Disk usage: " + usage + "%"); } } ``` 方法二:使用FileSystem类 ```java import java.nio.file.FileStore; import java.nio.file.Files; import java.nio.file.Paths; public class DiskSpace { public static void main(String[] args) throws Exception { FileStore fileStore = Files.getFileStore(Paths.get("/")); long totalSpace = fileStore.getTotalSpace(); long usableSpace = fileStore.getUsableSpace(); long usedSpace = totalSpace - usableSpace; System.out.println("Total space: " + totalSpace / (1024 * 1024) + "MB"); System.out.println("Usable space: " + usableSpace / (1024 * 1024) + "MB"); System.out.println("Used space: " + usedSpace / (1024 * 1024) + "MB"); double usage = (double) usedSpace / totalSpace * 100; System.out.println("Disk usage: " + usage + "%"); } } ``` 这两种方法都可以获取磁盘使用率,但是第二种方法可以获取文件系统的更多信息,如文件系统类型、文件系统名称等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值