阅读量:2
在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等。
以上就是关于“python cmd命令行如何查看磁盘空间”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm