Python查詢磁碟資訊,磁碟剩餘可用空間

2022-07-22 17:12:15 字數 1914 閱讀 1325

參考:

環境:python: 3.6

系統:server 2016

設計模組包:wmi   #pip install wmi

import

wmidef

get_disk_info():

"""獲取物理磁碟資訊。

"""tmplist =

c =wmi.wmi()

for physical_disk in

c.win32_diskdrive():

tmpdict ={}

tmpdict[

"caption

"] =physical_disk.caption

tmpdict[

"size

return

tmplist

defget_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

"] = int(logical_disk.size) / 1024 / 1024 / 1024tmpdict[

"usespace

"] = (int(logical_disk.size) - int(logical_disk.freespace)) / 1024 / 1024 / 1024tmpdict[

"freespace

"] = int(logical_disk.freespace) / 1024 / 1024 / 1024tmpdict[

"percent

"] =int(

100.0 * (int(logical_disk.size) - int(logical_disk.freespace)) /int(logical_disk.size))

return

tmplist

if__name__ == "

__main__":

disk =get_disk_info()

print

(disk)

#獲取磁碟資訊

fs =get_fs_info()

for f in

fs: disk_name = f['

caption

'] #

磁碟名 disktotal = f['

disktotal

'] #

磁碟大小 單位g

disk_usespace = f['

usespace

'] #

已用磁碟大小 單位g

disk_freespace = f['

freespace

'] #

剩餘可用磁碟大小 單位g

print('

磁碟名:{} 磁碟大小:{}g 已用空間:{}g 剩餘可用空間:{}g !!

'.format(disk_name,round(disktotal,2),round(disk_usespace,2),round(disk_freespace,2)))

檢視磁碟資訊磁碟空間

如果要檢視磁碟還剩多少空間,當然是用df的命令了。root localhost df h 檔案系統 容量 已用 可用 已用 掛載點 dev sda2 14g 11g 2.6g 82 dev sda1 99m 14m 81m 14 boot tmpfs 442m 275m 168m 63 dev sh...

遍歷磁碟資訊

h pragmaonce include include usingstd list typedefstruct drive info storage info drive info,pdrive info classcdiskcontrol cpp include diskcontrol.h cd...

磁碟情況查詢

1.查詢系統整體磁碟使用情況 df hdf lh2.檢視指定目錄的磁碟占用情況 du h 目錄 查詢執行目錄的磁碟占用情況,預設為當前目錄 例如檢視 opt目錄的磁碟占用情況,深度為1 3.磁碟情況 工作使用指令 3.1統計 home資料夾下檔案的個數 ls l home grep wc l3.2 ...