在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等。
上一篇:r语言爬虫与python爬虫在处理地理空间数据上有何差异
下一篇:linux python命令行如何管理虚拟环境
python









