【问题标题】:Find free disk space in python on OS/X在 OS/X 上的 python 中查找可用磁盘空间
【发布时间】:2010-10-21 17:27:45
【问题描述】:

我正在寻找我的 HD 上的空闲字节数,但在 python 上却遇到了麻烦。

我尝试了以下方法:

import os

stat = os.statvfs(path)
print stat.f_bsize * stat.f_bavail

但是,在 OS/X 上,它给了我 17529020874752 字节,大约 1.6 TB,这很好,但不幸的是,这不是真的。

得到这个数字的最佳方法是什么?

【问题讨论】:

标签: python diskspace


【解决方案1】:

尝试使用f_frsize 而不是f_bsize

>>> s = os.statvfs('/')
>>> (s.f_bavail * s.f_frsize) / 1024
23836592L
>>> os.system('df -k /')
Filesystem   1024-blocks     Used Available Capacity  Mounted on
/dev/disk0s2   116884912 92792320  23836592    80%    /

【讨论】:

  • 不知道为什么,但这仅在我将块大小 (1024) 更改为 512 时才对我有用。
  • 你使用的是什么操作系统/Python版本?
  • 不,不是。 statvfs module 在 Python 3.x 中已弃用/消失(这是一种将 statvfs 输出公开为元组查找索引而不是对象属性的旧方法),但我不使用我的答案中的模块。然而,os.statvfs 并没有被弃用——事实上它甚至在 3.6 中收到了更新:docs.python.org/3.6/library/os.html#os.statvfs。为现代 Python 进行调整所需的唯一更改是使用整数除法 (//),尽管 shutil.disk_usage 在另一个答案中肯定是一个更简单的选择!
【解决方案2】:

在 UNIX 上:

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)

用法:

>>> disk_usage('/')
usage(total=21378641920, used=7650934784, free=12641718272)
>>>

对于 Windows,您可以使用 psutil

【讨论】:

【解决方案3】:

在 python 3.3 及更高版本中,shutil 为您提供相同的功能

>>> import shutil
>>> shutil.disk_usage("/")
usage(total=488008343552, used=202575314944, free=260620050432)
>>> 

【讨论】:

  • 它工作,小巧优雅,完美(如果你有python 3.3+)
【解决方案4】:

Psutil module 也可以用。

>>> psutil.disk_usage('/')
usage(total=21378641920, used=4809781248, free=15482871808, percent=22.5)

文档可以在here找到。

【讨论】:

    【解决方案5】:
    def FreeSpace(drive):
        """ Return the FreeSape of a shared drive in bytes"""
        try:
            fso = com.Dispatch("Scripting.FileSystemObject")
            drv = fso.GetDrive(drive)
            return drv.FreeSpace
        except:
            return 0
    

    【讨论】:

      【解决方案6】:

      它不是独立于操作系统的,但这适用于 Linux,并且可能也适用于 OS X:

      打印命令.getoutput('df .').split('\n')[1].split()[3]

      它是如何工作的?它得到“df”的输出。命令,它为您提供有关当前目录所属分区的磁盘信息,将其分成两行(就像它打印到屏幕上一样),然后取第二行(通过在后面附加 [1]首先 split()),然后将 行拆分为不同的以空格分隔的部分,最后为您提供该列表中的第 4 个元素。

      >>> commands.getoutput('df .')
      'Filesystem           1K-blocks      Used Available Use% Mounted on\n/dev/sda3             80416836  61324872  15039168  81% /'
      
      >>> commands.getoutput('df .').split('\n')
      ['Filesystem           1K-blocks      Used Available Use% Mounted on', '/dev/sda3             80416836  61324908  15039132  81% /']
      
      >>> commands.getoutput('df .').split('\n')[1]
      '/dev/sda3             80416836  61324908  15039132  81% /'
      
      >>> commands.getoutput('df .').split('\n')[1].split()
      ['/dev/sda3', '80416836', '61324912', '15039128', '81%', '/']
      
      >>> commands.getoutput('df .').split('\n')[1].split()[3]
      '15039128'
      
      >>> print commands.getoutput('df .').split('\n')[1].split()[3]
      15039128
      

      【讨论】:

      • 这种方法不太可靠,因为没有可移植的方法可以从df 获取结构化输出。例如,只需尝试挂载一个 LVM 或 NFS 卷,它就会开始将其输出中断几行。
      【解决方案7】:

      怎么了

      import subprocess
      proc= subprocess.Popen( "df", stdout=subprocess.PIPE )
      proc.stdout.read()
      proc.wait()
      

      【讨论】:

      • 直接使用 Python 意味着它与操作系统无关。 :)
      • 它很脆弱,因为它依赖于外部应用程序来维护恒定的格式,而不是系统库。
      • 接受的答案也不与操作系统无关......我在 Windows 上安装的 2.6 没有os.statvfs
      • @Shane C. Mason:“依赖于外部应用程序”。外部应用程序由 POSIX 标准严格定义。它与库一样是操作系统的一部分,并且永远存在。
      • 路径没有设置怎么办?然后用户需要知道程序的完整路径。这是一个交易破坏者。
      猜你喜欢
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-25
      相关资源
      最近更新 更多