2017-07-02 9 views
-1

​​とMEMORYSTATUSEX()でRAMサイズが正常に取得できますが、ディスクサイズの合計(空き容量ではありませんが、一般的には総容量)の検索に問題があります。Windowsでディスク全体のサイズを取得するには?

+0

私はあなたが[こちら]答えを見つけることができると思います(https://stackoverflow.com/a/276934/5805982)RAM用です –

+0

@LexHobbit(メモリー) 、ディスクではありません。 – AlwaysQuestioning

+0

ああ、申し訳ありませんUはこれについて既に書いています=) –

答えて

2

のActiveStateは、Windows GetDiskFreeSpaceEx機能を使用して、このためrecipeを持っています。私はいくつかの限られたテストを行ったときに動作するように見えましたが、いくつかの潜在的な問題がありますので、少なくともPython 2.7+〜3.xで動作する、モジュールで。

@eryksunは、彼が明らかに​​を使用するトピックの専門家であるため、拡張機能のクレジット/責任の大半を受けなければなりません。

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

enter image description here

+0

Python3で0を返します – AlwaysQuestioning

関連する問題