1 # 咩咩咩咩咩咩 2 import os 3 import codecs 4 5 #get filename and size 6 j={} 7 for i in os.listdir("."): 8 m = os.path.join(".",i) 9 if os.path.isfile(m): 10 j[i]=os.path.getsize(m) 11 12 #sort the dictionary 13 sort_result = sorted(j.iteritems(),key = lambda asd:asd[1],reverse=False) 14 15 #print the result 16 f = codecs.open('flist.txt','w','utf-8') 17 for k in sort_result: 18 word = k[0]+" "+str(k[1])+"\n" 19 #print word, 20 if not isinstance(word,unicode): 21 word = unicode(word,"acsii") 22 #print isinstance(word,unicode) 23 #print word, 24 25 f.write(word) 26 f.close()
很简单~ 但是做个笔记
1 sorted
2 windows系统编码是gb2312,转utf-8需要先转unicode,再转utf-8,中文是没办法直接转换(在python2.x的情况下如此)