首页主机资讯python cmd命令行如何查看磁盘空间

python cmd命令行如何查看磁盘空间

时间2025-09-28 23:09:03发布访客分类主机资讯浏览652
导读:在Python中,你可以使用os模块和psutil库来查看磁盘空间 方法一:使用os模块 import os def get_disk_space( : total, used, free = os.statvfs('/' ...

在Python中,你可以使用os模块和psutil库来查看磁盘空间

方法一:使用os模块

import os

def get_disk_space():
    total, used, free = os.statvfs('/')
    total_space = total * os.path.getsize('/')
    used_space = used * os.path.getsize('/')
    free_space = free * os.path.getsize('/')
    
    return total_space, used_space, free_space

total_space, used_space, free_space = get_disk_space()
print(f"Total space: {
total_space}
 bytes")
print(f"Used space: {
used_space}
 bytes")
print(f"Free space: {
free_space}
 bytes")

方法二:使用psutil库 首先,你需要安装psutil库,可以使用以下命令安装:

pip install psutil

然后,你可以使用以下代码查看磁盘空间:

import psutil

def get_disk_space():
    total, used, free = psutil.disk_usage('/')
    return total, used, free

total_space, used_space, free_space = get_disk_space()
print(f"Total space: {
total_space}
 bytes")
print(f"Used space: {
used_space}
 bytes")
print(f"Free space: {
free_space}
     bytes")

这两种方法都可以帮助你查看磁盘空间。注意,这些方法返回的是字节单位,你可以根据需要将其转换为其他单位,如KB、MB、GB等。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: python cmd命令行如何查看磁盘空间
本文地址: https://pptw.com/jishu/712034.html
python cmd命令行可用于数据库操作吗 python cmd命令行怎样进行文件搜索

游客 回复需填写必要信息