python - 使用 python 计算 inode 使用百分比

标签 python linux

我正在尝试计算 python 中 inode 使用的百分比。 这是我的示例 python 代码

st = os.statvfs(path)
free  = (st.f_bavail * st.f_frsize) / 1024
total = (st.f_blocks * st.f_frsize) / 1024
used  = ((st.f_blocks - st.f_bfree) * st.f_frsize) / 1024
total_inode = st.f_files        # inodes 
free_inode = st.f_ffree   #free inodes 


# df -i /
Filesystem            Inodes   IUsed   IFree IUse% Mounted on
none                 8257011   69850 8187161    1% /

但是如何计算 df -i 命令中显示的 %inode?我试过“total_inodes-free_inodes/total_inodes”,但它给出了错误的使用百分比。

最佳答案

如果您使用 Python 2.x,int/int 会导致 int floored。您应该先将其转换为 float 。

>>> 1/2
0
>>> 1.0/2
0.5
>>> float(1)/2
0.5

print(float(total_inode - free_inode) / total_inode)

关于python - 使用 python 计算 inode 使用百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20727179/

相关文章:

python - 在python中将不同工作表中的列合并到单个工作表中

c++ - 代码块上的 Valgrind (linux)

c - WIFSTOPPED 无法正常工作

python - 从字典 Python 中选择最佳组

python - 可选的结束符号和使用正则表达式捕获的几个单词

python - 具有动态形状的变量 TensorFlow

python - 在 Python 中按复合键分组

linux - 我如何根据测试 .gz 的结果执行指令? Linux操作系统

linux - 使用 incrontab mv 文件生成 0 字节文件

linux - 如何在 FIND 命令结果中排除几个文件夹级别 - unix