[英]Get hard disk size in Python
我正在嘗試使用 Python 獲取硬盤驅動器大小和可用空間(我在 macOS 上使用 Python 2.7)。
我正在嘗試使用os.statvfs('/')
,尤其是使用以下代碼。 我在做什么正確嗎? 我應該使用變量giga
的哪個定義?
import os
def get_machine_storage():
result=os.statvfs('/')
block_size=result.f_frsize
total_blocks=result.f_blocks
free_blocks=result.f_bfree
# giga=1024*1024*1024
giga=1000*1000*1000
total_size=total_blocks*block_size/giga
free_size=free_blocks*block_size/giga
print('total_size = %s' % total_size)
print('free_size = %s' % free_size)
get_machine_storage()
編輯: statvfs
中不推薦使用 statvfs 3,您知道其他選擇嗎?
注意:正如評論部分中提到的一些人,此解決方案適用於Python 3.3及更高版本。 對於Python 2.7 ,最好使用psutil
庫,它有一個disk_usage
函數,包含有關總磁盤空間、已用磁盤空間和可用磁盤空間的信息:
import psutil
hdd = psutil.disk_usage('/')
print ("Total: %d GiB" % hdd.total / (2**30))
print ("Used: %d GiB" % hdd.used / (2**30))
print ("Free: %d GiB" % hdd.free / (2**30))
對於 Python 3.3 及更高版本,您可以使用shutil
模塊,它具有一個disk_usage
函數,返回一個命名元組,其中包含硬盤驅動器中的總空間、已用空間和可用空間。
您可以按如下方式調用該函數並獲取有關磁盤空間的所有信息:
import shutil
total, used, free = shutil.disk_usage("/")
print("Total: %d GiB" % (total // (2**30)))
print("Used: %d GiB" % (used // (2**30)))
print("Free: %d GiB" % (free // (2**30)))
輸出:
Total: 931 GiB
Used: 29 GiB
Free: 902 GiB
https://pypi.python.org/pypi/psutil
import psutil
obj_Disk = psutil.disk_usage('/')
print (obj_Disk.total / (1024.0 ** 3))
print (obj_Disk.used / (1024.0 ** 3))
print (obj_Disk.free / (1024.0 ** 3))
print (obj_Disk.percent)
代碼大致正確,但是您使用了錯誤的字段,這可能會在不同的系統上給您錯誤的結果。 正確的方法是:
>>> os.system('df -k /')
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/root 14846608 3247272 10945876 23% /
>>> disk = os.statvfs('/')
>>> (disk.f_bavail * disk.f_frsize) / 1024
10945876L
當您不知道如何處理函數的結果時,打印出類型會有所幫助。
print type(os.statvfs('/'))
返回<type 'posix.statvfs_result'>
這意味着它不是像 string 或 int 那樣的內置類實例。
您可以使用dir(instance)
檢查您可以對該實例執行的操作
print dir(os.statvfs('/'))
打印所有的屬性、函數、變量......
['__add__', '__class__', '__contains__', '__delattr__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__',
'__lt__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', 'f_bavail', 'f_bfree', 'f_blocks',
'f_bsize', 'f_favail', 'f_ffree', 'f_files', 'f_flag', 'f_frsize',
'f_namemax', 'n_fields', 'n_sequence_fields', 'n_unnamed_fields']
通過訪問其中一個變量,例如os.statvfs('/').f_ffree
您可以提取一個整數。
仔細檢查print type(os.statvfs('/').f_ffree)
,它確實打印了<type 'int'>
。
根據此處的答案顯示以 GiB 為單位的磁盤大小的單行解決方案:
>>> import shutil
>>> [f"{y}: {x//(2**30)} GiB" for x, y in zip(shutil.disk_usage('/'), shutil.disk_usage('/')._fields)]
['total: 228 GiB', 'used: 14 GiB', 'free: 35 GiB']
本帖所有回答只提供根分區的磁盤大小。 這是我為需要完整/整個硬盤大小的人編寫的代碼:
total = int()
used = int()
free = int()
for disk in psutil.disk_partitions():
if disk.fstype:
total += int(psutil.disk_usage(disk.mountpoint).total)
used += int(psutil.disk_usage(disk.mountpoint).used)
free += int(psutil.disk_usage(disk.mountpoint).free)
print(f'''
TOTAL DISK SPACE : {round(total / (1024.0 ** 3), 4)} GiB
USED DISK SPACE : {round(used / (1024.0 ** 3), 4)} GiB
FREE DISK SPACE : {round(free / (1024.0 ** 3), 4)} GiB
''')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.