Find free disk space in python on OS/X

試著忘記壹切 提交于 2019-11-27 19:04:16

Try using f_frsize instead of 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%    /

On 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)

Usage:

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

For Windows you might use psutil.

In python 3.3 and above shutil provides you the same feature

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

Psutil module can also be used.

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

documentation can be found here.

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

It's not OS-independent, but this works on Linux, and probably on OS X as well:

print commands.getoutput('df .').split('\n')[1].split()[3]

How does it work? It gets the output of the 'df .' command, which gives you disk information about the partition of which the current directory is a part, splits it into two lines (just as it is printed to the screen), then takes the second line of that (by appending [1] after the first split()), then splits that line into different whitespace-separated pieces, and, finally, gives you the 4th element in that list.

>>> 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

What's wrong with

import subprocess
proc= subprocess.Popen( "df", stdout=subprocess.PIPE )
proc.stdout.read()
proc.wait()
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!