我在某些目录上执行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
命令,因为用户希望在主题词上看到长列表
答案 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的文件列表,如何使用find
? find -maxdepth 1 -size +100M