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,内存,占用,储存,大小
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
微软Silverlight技术魅力初体验
2008-11-05 11:16:00
![](https://img.aspxhome.com/file/uploadpic/200811/5/47fe8a0e93ec22f6ab6457e1-97.jpg)
“您无权查看该网页”的原因和解决方法
2008-03-24 16:57:00
详解git submodule使用以及注意事项
2023-04-30 21:11:30
![](https://img.aspxhome.com/file/2023/6/112066_0s.jpg)
python3.7调试的实例方法
2022-09-22 17:16:24
![](https://img.aspxhome.com/file/2023/8/82758_0s.png)
Python访问Redis的详细操作
2022-03-13 13:22:25
![](https://img.aspxhome.com/file/2023/5/98785_0s.png)
Golang HTTP 服务平滑重启及升级的思路
2024-02-02 18:05:42
![](https://img.aspxhome.com/file/2023/9/107369_0s.png)
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
![](https://img.aspxhome.com/file/2023/1/69031_0s.png)
在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
![](https://img.aspxhome.com/file/2023/2/132322_0s.png)
vue后台管理添加多语言功能的实现示例
2024-04-29 13:08:22
Django怎么在admin后台注册数据库表
2024-01-26 03:12:50
![](https://img.aspxhome.com/file/2023/3/134493_0s.png)
python的staticmethod与classmethod实现实例代码
2022-10-02 23:56:45
MySQL系列多表连接查询92及99语法示例详解教程
2024-01-21 03:05:33
![](https://img.aspxhome.com/file/2023/9/115559_0s.png)
Python统计纯文本文件中英文单词出现个数的方法总结【测试可用】
2023-05-14 08:03:02