#!/usr/bin/env pythonimport osimport sysimport operatordef getfile (DIR1): d = os.walk (DIR1) dic = {} for a,b,c in d: for i in c: fn = os.path.join (a,i) f_size = os.path.getsize (FN) dic[fn] = f_size return dicif __name__ == ' __main__ ': dir1 = sys.argv[1] dic = getfile (DIR1) sorted_dic = sorted ( Dic.iteritems (), Key=operator.itemgetter (1), reverse=true) for k,v in Sorted_dic[:10]: print "%s===>%s" % (k,v)
-
Sorted is a sort function, Dic.iteritems () returns an object that can be iterated, the key is specified according to the K or y column, Operator.itemgetter (1) is sorted according to the dictionary Y-stream, Operator.itemgetter (0) indicates that the order is sorted according to the Dictionary k column, reverse=true indicates a reverse sort, and false by default. SORTED_DIC[:10] indicates that the first 10 digits of the list are listed.
This article is from the "Stranded," blog, please be sure to keep this source http://yasar.blog.51cto.com/9120455/1877507
Python dictionary sort