How does python use the wmi module to obtain hard disk information in windows?
This example describes how to use the wmi module in python to obtain hard disk information under windows. Share it with you for your reference. The specific implementation method is as follows:
?
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 ():
"""
Obtain physical disk information.
"""
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 ():
"""
Obtain the file system information.
It contains the partition size, used, available, usage, and mount point information.
"""
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
I hope this article will help you with Python programming.