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设计】。

相关文章

Python3实现连接SQLite数据库的方法

本文实例讲述了Python3实现连接SQLite数据库的方法,对于Python的学习有不错的参考借鉴价值。分享给大家供大家参考之用。具体方法如下: 实例代码如下: import sq...

Python基本语法之运算符功能与用法详解

本文实例讲述了Python基本语法之运算符功能与用法。分享给大家供大家参考,具体如下: 前言 在前面的博文介绍了Python的数据结构之后,接下来结合Python操作符来对Python程...

Python 经典面试题 21 道【不可错过】

到底什么是Python? •Python是一种解释性语言。Python代码在运行之前不需要编译。其它解释性语言还包括PHP和Ruby。 •Python是...

python控制台显示时钟的示例

复制代码 代码如下:#!/usr/bin/env python# coding: utf-8### show time in console#import sysimport time...

在cmder下安装ipython以及环境的搭建

在cmder下安装ipython以及环境的搭建

打开cmder 1.移动到D盘 输入命令:D: 2.创建文件夹 λ mkdir myApp 3.创建python自带的虚拟环境 λ python -m venv...