python检测空间储存剩余大小和指定文件夹内存占用的实例

yipeiwu_com6年前Python基础

1、检测指定路径下所有文件所占用内存

import os
def check_memory(path, style='M'):
 i = 0
 for dirpath, dirname, filename in os.walk(path):
  for ii in filename:
   i += os.path.getsize(os.path.join(dirpath,ii))
 if style == 'M':
  memory = i / 1024. / 1024.
  print '%.2f MB' % memory
 else:
  memory = i / 1024. / 1024./ 1024.
  print '%.4f GB' % memory

2、检测指定路径剩余储存空间大小

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/1024.

这个适用于unix系统下,windows系统下 os 无 statvfs 属性。

def disk_stat(path):
 import os
 hd={}
 disk = os.statvfs(path)
 percent = (disk.f_blocks - disk.f_bfree) * 100 / (disk.f_blocks -disk.f_bfree + disk.f_bavail) + 1
 return percent
print disk_stat('.')

以上这篇python检测空间储存剩余大小和指定文件夹内存占用的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python访问系统环境变量的方法

本文实例讲述了python访问系统环境变量的方法。分享给大家供大家参考。具体如下: #-------------------------------- # Name: en...

python3.5 + PyQt5 +Eric6 实现的一个计算器代码

python3.5 + PyQt5 +Eric6 实现的一个计算器代码

目前可以实现简单的计算。计算前请重置,设计的时候默认数字是0,学了半天就做出来个这么个结果,bug不少。 python3.5 + PyQt5 +Eric6 在windows7 32位系统...

详解django2中关于时间处理策略

详解django2中关于时间处理策略

一、django中数据模型关于时间字段的认识 1、 DateField :可以记录年月日,映射到数据库是 date 类型 2、 DateTimeField :可以记录年月日时分秒,映射到...

分享6个隐藏的python功能

小编在以前给大家介绍过python一些很少用到的功能,这次我们给大家分享了6个隐藏的python功能,学习下。 在python的设计哲学中,有这么一条内容:“Simple is bett...

Python面向对象程序设计类变量与成员变量、类方法与成员方法用法分析

本文实例讲述了Python面向对象程序设计类变量与成员变量、类方法与成员方法用法。分享给大家供大家参考,具体如下: 类变量与成员变量 在类中声明的变量我们称之为类变量[静态成员变量],...