从ls -1hl命令的输出中查找超过100 MB的文件

时间:2014-08-10 06:59:41

标签: python linux

我在某些目录上执行ls -1lh命令并在GUI上显示输出。

ls命令中使用的选项的描述

1 --> print one file/directory in one line
l --> long listing
h --> human readable format (converting in KB, MB, G)

现在我从ls -1lh命令中提取了大小: -

size = ["100","9.6K","12M","79M","679M","222K","23","132M","3G","1.3G"]

现在我想知道python中的大小超过100M

我的方法是什么?我必须使用ls -1lh命令,因为用户希望在主题词上看到长列表

1 个答案:

答案 0 :(得分:0)

units = dict(K=1024, M=1024*1024, G=1024*1024*1024)
def parse_size(s):
    times = units.get(s[-1:], 1)
    if times == 1:
        return float(s)
    else:
        return float(s[:-1]) * times

用法:

>>> size = ["100","9.6K","12M","79M","679M","222K","23","132M","3G","1.3G"]
>>> threshold = parse_size('100M')
>>> print [s for s in size if parse_size(s) >= threshold]
['679M', '132M', '3G', '1.3G']

BTW,如果你想获得大于100MB的文件列表,如何使用findfind -maxdepth 1 -size +100M