0

Is there a way I get can the following disk statistics in Python without using PSUtil?

  • Total disk space
  • Used disk space
  • Free disk space

All the examples I have found seem to use PSUtil which I am unable to use for this application.

My device is a Raspberry PI with a single SD card. I would like to get the total size of the storage, how much has been used and how much is remaining.

Please note I am using Python 2.7.

Remotec
  • 10,304
  • 25
  • 105
  • 147

2 Answers2

3

Can you check out this

import os
from collections import namedtuple

_ntuple_diskusage = namedtuple('usage', 'total used free')

def disk_usage(path):
    """Return disk usage statistics about the given path.

    Returned valus is a named tuple with attributes 'total', 'used' and
    'free', which are the amount of total, used and free space, in bytes.
    """
    st = os.statvfs(path)
    free = st.f_bavail * st.f_frsize
    total = st.f_blocks * st.f_frsize
    used = (st.f_blocks - st.f_bfree) * st.f_frsize
    return _ntuple_diskusage(total, used, free)

source

Jeril
  • 7,858
  • 3
  • 52
  • 69
1

You can do this with the os.statvfs function.

Dan D.
  • 73,243
  • 15
  • 104
  • 123