我可以通過和MEMORYSTATUSEX()
獲得RAM大小,但是我無法找到任何有關總磁盤大小(不是可用空間,但總體容量)的任何內容。如何在Windows上獲取總磁盤大小?
-1
A
回答
2
ActiveState公司有這個使用Windows GetDiskFreeSpaceEx
功能recipe。它似乎工作時,我做了一些有限的測試,但它有一些潛在的問題,所以這是一個大大改進,更防彈的版本,至少在Python 2.7 +至3.x),並只使用內置的,在模塊中。
@eryksun值得大多數信譽/責任的增強,因爲(s)他顯然是使用的專家的專家。
import os
import collections
import ctypes
import sys
import locale
locale.setlocale(locale.LC_ALL, '') # set locale to default to get thousands separators
PULARGE_INTEGER = ctypes.POINTER(ctypes.c_ulonglong) # Pointer to large unsigned integer
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
kernel32.GetDiskFreeSpaceExW.argtypes = (ctypes.c_wchar_p,) + (PULARGE_INTEGER,) * 3
class UsageTuple(collections.namedtuple('UsageTuple', 'total, used, free')):
def __str__(self):
# Add thousands separator to numbers displayed
return self.__class__.__name__ + '(total={:n}, used={:n}, free={:n})'.format(*self)
def disk_usage(path):
if sys.version_info < (3,): # Python 2?
saved_conversion_mode = ctypes.set_conversion_mode('mbcs', 'strict')
else:
try:
path = os.fsdecode(path) # allows str or bytes (or os.PathLike in Python 3.6+)
except AttributeError: # fsdecode() not added until Python 3.2
pass
# Define variables to receive results when passed as "by reference" arguments
_, total, free = ctypes.c_ulonglong(), ctypes.c_ulonglong(), ctypes.c_ulonglong()
success = kernel32.GetDiskFreeSpaceExW(
path, ctypes.byref(_), ctypes.byref(total), ctypes.byref(free))
if not success:
error_code = ctypes.get_last_error()
if sys.version_info < (3,): # Python 2?
ctypes.set_conversion_mode(*saved_conversion_mode) # restore conversion mode
if not success:
windows_error_message = ctypes.FormatError(error_code)
raise ctypes.WinError(error_code, '{} {!r}'.format(windows_error_message, path))
used = total.value - free.value
return UsageTuple(total.value, used, free.value)
if __name__ == '__main__':
print(disk_usage('C:/'))
輸出示例:
UsageTuple(total=102,025,392,128, used=66,308,366,336, free=35,717,025,792)
-1
然後你應該使用這段代碼。
import win32com.client as com
def TotalSize(drive):
""" Return the TotalSize of a shared drive [GB]"""
try:
fso = com.Dispatch("Scripting.FileSystemObject")
drv = fso.GetDrive(drive)
return drv.TotalSize/2**30
except:
return 0
def FreeSpace(drive):
""" Return the FreeSape of a shared drive [GB]"""
try:
fso = com.Dispatch("Scripting.FileSystemObject")
drv = fso.GetDrive(drive)
return drv.FreeSpace/2**30
except:
return 0
drive = r'C:'
print 'TotalSize of %s = %d GB' % (drive, TotalSize(drive))
print 'FreeSapce on %s = %d GB' % (drive, FreeSapce(drive))
+0
在Python3上返回0 – AlwaysQuestioning
相關問題
- 1. XE4獲取總磁盤大小
- 2. 獲取磁盤上的文件大小
- 3. 如何在linux上獲取磁盤上的文件大小?
- 4. 如何獲取便攜式磁盤的磁盤大小?
- 5. 如何獲取Windows機器的硬盤總大小?
- 6. 獲取磁盤的大小堅持
- 7. 正確獲取磁盤大小
- 8. 大小和磁盤大小
- 9. 文件夾大小如何大於磁盤上的大小?
- 10. 如何在Windows上獲取磁盤標識符?
- 11. 如何設置RollingFileAppender的最大總磁盤大小?
- 12. OSX:獲取磁盤上文件/目錄的大小
- 13. 獲取Powershell中小文件的「磁盤大小」
- 14. 從ioctl調用大於2TB的veritas卷獲取磁盤大小
- 15. 磁盤上的InfluxDB存儲大小
- 16. 磁盤上的Java目錄大小?
- 17. 如何將磁盤大小配額放在git存儲庫上?
- 18. 如何在線上調整xen PV guest磁盤的大小?
- 19. UNFORMAT磁盤大小串
- 20. Redis磁盤大小巧合?
- 21. 磁盤大小不正確
- 22. 磁盤大小問題
- 23. 如何在Windows中獲取每個進程的磁盤活動
- 24. 如何在Windows 2000上以字節爲單位獲取硬盤大小?
- 25. 如何獲得視頻PHAsset磁盤文件大小?
- 26. C++ Qt - 獲取物理磁盤大小(Win32)
- 27. 如何確定Win32上的最佳磁盤IO塊大小?
- 28. 如何增加Google Cloud上我的實例磁盤的大小?
- 29. 在磁盤上存儲大量不同大小的對象
- 30. 如何用R獲取windows機器的磁盤空間?
我認爲你可以找到答案[這裏](https://stackoverflow.com/a/276934/5805982) –
@LexHobbit這對內存(RAM) ,而不是磁盤。 – AlwaysQuestioning
哦,對不起,你已經寫了這個=) –