Python實現(xiàn)獲取磁盤剩余空間的2種方法
本文實例講述了Python實現(xiàn)獲取磁盤剩余空間的2種方法。分享給大家供大家參考,具體如下:
方法1:
import ctypes import os import platform import sys def get_free_space_mb(folder): """ Return folder/drive free space (in bytes) """ if platform.system() == 'Windows': free_bytes = ctypes.c_ulonglong(0) ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(folder), None, None, ctypes.pointer(free_bytes)) return free_bytes.value/1024/1024/1024 else: st = os.statvfs(folder) return st.f_bavail * st.f_frsize/1024/1024 print(get_free_space_mb('C:\\'),'GB')
方法2:
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 FreeSpace of a shared drive [GB]""" try: fso = com.Dispatch("Scripting.FileSystemObject") drv = fso.GetDrive(drive) return drv.FreeSpace/2**30 except: return 0 workstations = ['dolphins'] print ('Hard drive sizes:') for compName in workstations: drive = '\\\\' + compName + '\\c$' print ('*************************************************\n') print (compName) print ('TotalSize of %s = %f GB' % (drive, TotalSize(drive))) print ('FreeSpace on %s = %f GB' % (drive, FreeSpace(drive))) print ('*************************************************\n')
運行效果如下圖:
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
python利用tkinter實現(xiàn)圖片格式轉(zhuǎn)換的示例
這篇文章主要介紹了python利用tkinter實現(xiàn)圖片格式轉(zhuǎn)換,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-09-09Python實現(xiàn)將橫表和縱表任意轉(zhuǎn)換的兩種方法
在日常做數(shù)據(jù)分析,接收到最多的表格是縱表,每個字段變量都有很長數(shù)據(jù)的長表,我們稱之為縱向數(shù)據(jù),但是,有時候,我們也會遇到橫表,對于橫向數(shù)據(jù),我們會數(shù)據(jù)轉(zhuǎn)化,將其轉(zhuǎn)化為縱向數(shù)據(jù),感興趣的同學跟著小編一起來學習吧2023-12-12python實現(xiàn)監(jiān)控阿里云賬戶余額功能
這篇文章主要介紹了python實現(xiàn)監(jiān)控阿里云賬戶余額功能,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12使用Pycharm創(chuàng)建一個Django項目的超詳細圖文教程
Django是比較經(jīng)典的Python web框架,最近剛好在項目中用到了Django,所以下面這篇文章主要給大家介紹了關(guān)于使用Pycharm創(chuàng)建一個Django項目的超詳細圖文教程,文中介紹的非常詳細,需要的朋友可以參考下2022-08-08