【问题标题】:How to get disk space total, used and free using Python 2.7 without PSUtil如何在没有 PSUtil 的情况下使用 Python 2.7 获取总磁盘空间、已使用和可用磁盘空间
【发布时间】:2019-01-31 18:29:51
【问题描述】:

有没有一种方法可以在不使用 PSUtil 的情况下在 Python 中获得以下磁盘统计信息?

  • 总磁盘空间
  • 已用磁盘空间
  • 可用磁盘空间

我发现的所有示例似乎都使用了我无法用于此应用程序的 PSUtil。

我的设备是带有单个 SD 卡的 Raspberry PI。我想获取存储的总大小、已使用多少以及剩余多少。

请注意我使用的是 Python 2.7。

【问题讨论】:

    标签: python python-2.7 raspberry-pi


    【解决方案1】:

    你可以看看这个

    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

    【讨论】:

    • 我认为这只是Python3?我会修改我的问题,说我需要 Python 2.7。我的错。
    【解决方案2】:

    您可以使用os.statvfs 函数来做到这一点。

    【讨论】:

      猜你喜欢
      • 2019-12-12
      • 1970-01-01
      • 1970-01-01
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 2010-11-26
      • 2012-11-15
      相关资源
      最近更新 更多