我试图使用Python(我正在使用macOS和Python 2.7)获取硬盘驱动器大小和可用空间。
我尝试使用 os.statvfs('/')
,特别是下面的代码。
我现在的做法正确吗?我应该使用哪个变量定义来表示“giga”?
import os
def get_machine_storage():
result=os.statvfs('/')
block_size=result.f_frsize
total_blocks=result.f_blocks
free_blocks=result.f_bfree
# giga=1024*1024*1024
giga=1000*1000*1000
total_size=total_blocks*block_size/giga
free_size=free_blocks*block_size/giga
print('total_size = %s' % total_size)
print('free_size = %s' % free_size)
get_machine_storage()
编辑:
statvfs
在Python 3中已被弃用,您知道有什么替代品吗?
shutil
模块中的disk_usage
。 - Vasilis G.