python cmd命令行如何查看磁盘空间
在Python中,你可以使用os
模块和psutil
库来查看磁盘空间
方法一:使用os模块
import osdef 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_spacetotal_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 psutildef get_disk_space():total, used, free = psutil.disk_usage('/')return total, used, freetotal_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等。