Get hard disk size in Python

前端 未结 4 1039
闹比i
闹比i 2020-12-23 20:01

I am trying to get the hard drive size and free space using Python (I am using Python 2.7 with macOS).

I am trying with os.statvfs(\'/\'), especially wi

4条回答
  •  忘掉有多难
    2020-12-23 20:31

    The code is about right, but you're using wrong fields, which may give you the wrong results on a different system. The correct way would be:

    >>> os.system('df -k /')
    Filesystem     1K-blocks    Used Available Use% Mounted on
    /dev/root       14846608 3247272  10945876  23% /
    
    >>> disk = os.statvfs('/')
    >>> (disk.f_bavail * disk.f_frsize) / 1024
    10945876L
    

提交回复
热议问题