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

作者:晓东邪 时间:2022-10-30 06:52:51 

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('.')

来源:https://blog.csdn.net/xiaodongxiexie/article/details/74545561

标签:python,内存,占用,储存,大小
0
投稿

猜你喜欢

  • 微软Silverlight技术魅力初体验

    2008-11-05 11:16:00
  • “您无权查看该网页”的原因和解决方法

    2008-03-24 16:57:00
  • 详解git submodule使用以及注意事项

    2023-04-30 21:11:30
  • python3.7调试的实例方法

    2022-09-22 17:16:24
  • Python访问Redis的详细操作

    2022-03-13 13:22:25
  • Golang HTTP 服务平滑重启及升级的思路

    2024-02-02 18:05:42
  • Oracle In和exists not in和not exists的比较分析

    2009-08-27 10:07:00
  • IE9一个非常牛的“bug”

    2010-05-07 12:45:00
  • Python selenium键盘鼠标事件实现过程详解

    2021-09-16 05:26:23
  • 用层模拟下拉列表框

    2013-07-01 01:19:00
  • python使用pyodbc连接sqlserver

    2021-12-24 06:06:17
  • 在Python安装MySQL支持模块的方法

    2024-01-24 06:58:52
  • Python如何处理大数据?3个技巧效率提升攻略(推荐)

    2022-04-02 10:03:03
  • 浅谈MySQL在cmd和python下的常用操作

    2024-01-16 13:11:17
  • 游戏开发进阶Unity网格(Mesh\\动态合批\\骨骼动画\\蒙皮)

    2022-03-18 11:20:26
  • vue后台管理添加多语言功能的实现示例

    2024-04-29 13:08:22
  • Django怎么在admin后台注册数据库表

    2024-01-26 03:12:50
  • python的staticmethod与classmethod实现实例代码

    2022-10-02 23:56:45
  • MySQL系列多表连接查询92及99语法示例详解教程

    2024-01-21 03:05:33
  • Python统计纯文本文件中英文单词出现个数的方法总结【测试可用】

    2023-05-14 08:03:02
  • asp之家 网络编程 m.aspxhome.com