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实现指定字符串补全空格、前面填充0的方法

Python zfill()方法返回指定长度的字符串,原字符串右对齐,前面填充0。 zfill()方法语法:str.zfill(width) 参数width -- 指定字符串的长度。原字...

TensorFlow查看输入节点和输出节点名称方式

TensorFlow 定义输入节点名称input_name: with tf.name_scope('input'): bottleneck_input = tf.place...

Python创建日历实例

本文讲述了Python创建日历的方法,与以往不同的是,本文实例不使用Python提供的calendar实现,相信对大家的Python程序设计有一定的借鉴价值。 此程序在windows下测...

Python安装Imaging报错:The _imaging C module is not installed问题解决方法

今天写Python程序上传图片需要用到PIL库,于是到http://www.pythonware.com/products/pil/#pil117下载了一个1.1.7版本的,我用的是Ce...

使用Python刷淘宝喵币(低阶入门版)

这两天因为双十一来临,到处收集喵币,反反复复的点击操作搞得我十分头痛,遂产生了写个脚本自动点击的想法。 【低阶入门版本】之中不牵扯图像文字转换,或者图像匹配的问题,只是简单的屏幕开屏、点...