-
Daniel Meißner authoredDaniel Meißner authored
disk.py 1.35 KiB
# -*- encoding: utf-8 -*-
import os
class Disk(object):
"""
Class for disk operations.
"""
@staticmethod
def free_disk_space(path):
"""
Calculate free disk space for given path.
Returned available_bytes or None when path not exists or writable.
"""
if Disk.path_writeble(path):
stats = os.statvfs(path)
available_bytes = (stats.f_bavail * stats.f_frsize)
return available_bytes
else:
return None
@staticmethod
def path_writeble(path):
"""
Check if a given path is writable for user.
"""
if os.path.exists(path):
return os.access(path, os.W_OK)
elif os.access(os.path.dirname(path), os.W_OK) and \
os.access(path, os.W_OK):
return True
else:
return False
@staticmethod
def sizeof_fmt(num, suffix="B"):
"""
Convert number of bytes into human readable format.
Source: https://web.archive.org/web/20111010015624/http://blogmag.net/blog/read/38/Print_human_readable_file_size
"""
for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]:
if abs(num) < 1024.0:
return "%3.1f %s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f %s%s" % (num, "Yi", suffix)