I'm doing ls -1lh
command on certain directories and displaying the output on the GUI.
description of options used in ls command
1 --> print one file/directory in one line
l --> long listing
h --> human readable format (converting in KB, MB, G)
now i have sizes extracted from ls -1lh
command:-
size = ["100","9.6K","12M","79M","679M","222K","23","132M","3G","1.3G"]
now i want to find out what sizes are more than 100M in python
What could be my approach? i have to use ls -1lh
command because user wants to see the long listing on the directoires
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
Usage:
>>> 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, how about using find
if you want to get list of files that is larger than 100MB? find -maxdepth 1 -size +100M
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.