Domanda

sto cercando il numero di byte liberi sul mio HD, ma hanno difficoltà a farlo in python.

Ho provato quanto segue:

import os

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

Ma, su OS / X mi dà un 17529020874752 byte, che è di circa circa 1,6 TB, che sarebbe molto bello, ma purtroppo non è propriamente vero.

Qual è il modo migliore per arrivare a questa cifra?

È stato utile?

Soluzione

Prova a usare f_frsize invece di 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%    /

Altri suggerimenti

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

Utilizzo:

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

Per Windows è possibile utilizzare psutil .

In Python 3.3 e superiori shutil vi offre la stessa caratteristica

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

psutil modulo può anche essere usato.

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

documentazione può essere trovato qui .

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

Non è indipendente dal sistema operativo, ma questo funziona su Linux, e probabilmente su OS X, così:

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

Come funziona? Ottiene l'uscita del 'df.' comando, che fornisce informazioni sul disco sulla partizione di cui la directory corrente è una parte, si divide in due linee (proprio come viene stampato sullo schermo), poi prende la seconda linea di che (aggiungendo [1] dopo il primo split ()), quindi si divide che linea in diverse separate da spazi: pezzi, e, infine, si dà il 4 ° elemento in tale elenco.

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

Cosa c'è di sbagliato con il

import subprocess
proc= subprocess.Popen( "df", stdout=subprocess.PIPE )
proc.stdout.read()
proc.wait()
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top