我想知道我的硬盘上有多少空闲字节,但在Python中做起来有些困难。
我尝试了以下方法:
import os
stat = os.statvfs(path)
print stat.f_bsize * stat.f_bavail
但是,在OS/X上,它给了我一个17529020874752字节的数字,大约是1.6TB,这听起来很棒,但不幸的是并不完全正确。
如何最好地得出这个数字?
我想知道我的硬盘上有多少空闲字节,但在Python中做起来有些困难。
我尝试了以下方法:
import os
stat = os.statvfs(path)
print stat.f_bsize * stat.f_bavail
但是,在OS/X上,它给了我一个17529020874752字节的数字,大约是1.6TB,这听起来很棒,但不幸的是并不完全正确。
如何最好地得出这个数字?
尝试使用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% /
statvfs
模块已被弃用或删除(这是一种将statvfs输出作为元组索引公开,而不是作为对象属性的旧方法),但我在答案中没有使用该模块。然而,os.statvfs
并没有被弃用——事实上,它甚至在3.6中得到更新:https://docs.python.org/3.6/library/os.html#os.statvfs。为了适应现代Python,唯一需要修改的是使用整数除法(`//`), 不过像另一个答案中所述的shutil.disk_usage
肯定是更简单的选择! - Nicholas Riley在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。
df
命令的其他解决方案更好。 - guettli在 Python 3.3 及以上版本,shutil 提供了相同的功能。
>>> import shutil
>>> shutil.disk_usage("/")
usage(total=488008343552, used=202575314944, free=260620050432)
>>>
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
它不是独立于操作系统的,但在Linux上可以工作,并且很可能在OS X上也可以:
print commands.getoutput('df .').split('\n')[1].split()[3]
它是如何工作的?它获取'df .'命令的输出,该命令提供有关当前目录所在分区的磁盘信息,将其分成两行(就像打印到屏幕上一样),然后获取其中的第二行(通过在第一个拆分()之后附加[1]),然后将该行拆分为不同的以空格分隔的部分,最后给出该列表中的第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еҚ·пјҢе®ғе°ҶејҖе§Ӣе°Ҷиҫ“еҮәеҲҶејҖи·Ёи¶ҠеҮ иЎҢгҖӮ - conny出了什么问题?
import subprocess
proc= subprocess.Popen( "df", stdout=subprocess.PIPE )
proc.stdout.read()
proc.wait()
os.statvfs
。 - Roman Starkov