萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> python使用wmi模塊獲取windows下硬盤信息的方法

python使用wmi模塊獲取windows下硬盤信息的方法

  本文實例講述了python使用wmi模塊獲取windows下硬盤信息的方法。分享給大家供大家參考。具體實現方法如下:

  ?

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12

  13

  14

  15

  16

  17

  18

  19

  20

  21

  22

  23

  24

  25

  26

  27

  28

  29

  30

  31

  32

  33

  34

  35

  36

  37

  38

  39

  40

  41

  42

  43

  44

  45# -*- coding: utf-8 -*-

  #import

  ########################################################################

  import os, sys

  import time

  import wmi

  ########################################################################

  #function

  ########################################################################

  def get_disk_info():

  """

  獲取物理磁盤信息。

  """

  tmplist = []

  c = wmi.WMI ()

  for physical_disk in c.Win32_DiskDrive ():

  tmpdict = {}

  tmpdict["Caption"] = physical_disk.Caption

  tmpdict["Size"] = long(physical_disk.Size)/1024/1024/1024

  tmplist.append(tmpdict)

  return tmplist

  def get_fs_info() :

  """

  獲取文件系統信息。

  包含分區的大小、已用量、可用量、使用率、掛載點信息。

  """

  tmplist = []

  c = wmi.WMI ()

  for physical_disk in c.Win32_DiskDrive ():

  for partition in physical_disk.associators ("Win32_DiskDriveToDiskPartition"):

  for logical_disk in partition.associators ("Win32_LogicalDiskToPartition"):

  tmpdict = {}

  tmpdict["Caption"] = logical_disk.Caption

  tmpdict["DiskTotal"] = long(logical_disk.Size)/1024/1024/1024

  tmpdict["UseSpace"] = (long(logical_disk.Size)-long(logical_disk.FreeSpace))/1024/1024/1024

  tmpdict["FreeSpace"] = long(logical_disk.FreeSpace)/1024/1024/1024

  tmpdict["Percent"] = int(100.0*(long(logical_disk.Size)-long(logical_disk.FreeSpace))/long(logical_disk.Size))

  tmplist.append(tmpdict)

  return tmplist

  if __name__ == "__main__":

  disk = get_disk_info()

  print disk

  print '--------------------------------------'

  fs = get_fs_info()

  print fs

  希望本文所述對大家的Python程序設計有所幫助。

copyright © 萬盛學電腦網 all rights reserved