python对磁盘操作
查看文件夹的大小
查看目录挂载点的 使用情况
Disk_Usage = namedtuple("usage", "total used free percent")
class DiskSpaceUtil(object): @staticmethod def get_dir_size(dir_path): """unit byte""" size = 0l for root, dirs, file_names in os.walk(dir_path): size += sum([os.path.getsize(os.path.join(root, file_name)) for file_name in file_names]) return size @staticmethod def get_dir_usage(dir_path): """The usage of this dir_path`s mount path""" """Return disk usage associated with path.""" st = os.statvfs(dir_path) free = (st.f_bavail * st.f_frsize) total = (st.f_blocks * st.f_frsize) used = (st.f_blocks - st.f_bfree) * st.f_frsize try: percent = ret = (float(used) / total) * 100 except ZeroDivisionError: percent = 0 return Disk_Usage(total, used, free, round(percent, 1))
References:
http://blog.csdn.net/magic_zj00/article/details/7207445