Created
April 15, 2020 07:48
-
-
Save cuahutli/5879301907ce648d1c904ba5fd627faf to your computer and use it in GitHub Desktop.
Get Information about disk, Total, Free and Used Space
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ctypes | |
import platform | |
import os | |
class HardDriveSpaceException(Exception): | |
def __init__(self, value): | |
self.parameter = value | |
def __str__(self): | |
return repr(self.parameter) | |
def get_disk_info(folder, format="GB"): | |
fConstants = {"GB": 1073741824, | |
"MB": 1048576, | |
"KB": 1024, | |
"B": 1 | |
} | |
if platform.system() == 'Windows': | |
print("windows system") | |
free_bytes = ctypes.c_ulonglong(0) | |
total_bytes = ctypes.c_ulonglong(0) | |
ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(folder), None, ctypes.pointer(total_bytes), ctypes.pointer(free_bytes)) | |
free = int(free_bytes.value/fConstants[format.upper()]) | |
total = int(total_bytes.value/fConstants[format.upper()]) | |
used = total - free | |
disk_info = { | |
'total': total, | |
'free': free, | |
'used': used | |
} | |
return disk_info | |
else: | |
st = os.statvfs("/") | |
print("Linux system") | |
free = int((st.f_bavail * st.f_frsize)/fConstants[format.upper()]) | |
total = int((st.f_blocks * st.f_frsize)/fConstants[format.upper()]) | |
used = int((st.f_blocks - st.f_bfree) * st.f_frsize/fConstants[format.upper()]) | |
disk_info = { | |
'total': total, | |
'free': free, | |
'used': used | |
} | |
return disk_info | |
if __name__ == "__main__": | |
try: | |
limit = 207 | |
disk_info = get_disk_info(r"c:") | |
print(disk_info) | |
if disk_info['free'] <= limit: | |
raise HardDriveSpaceException("Quedan solo {free} GB de espacio en el Disco Duro, Favor de liberar espacio".format(free=disk_info['free'])) | |
except HardDriveSpaceException as e: | |
print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment