【Python】CDay-1 优化!对自个的反省
2013-07-29 00:00
148 查看
编程实现以下功能并进行最大化的优化:遍历指定目录下的所有文件,找出其中占用空间最大的前3个文件。
运行截屏:
利用ConfigParser,将上述题目中产生的结果按照cdays+1-my.ini格式存储到文件cdays+1-result.txt中。
cdays+1-my.ini内容为:
源代码
运行结果
parseresult.txt如下:
#!/usr/bin/env python #-*- coding: utf-8 -*- '''selectbiggestfile.py @note: 使用os.stat获取相关信息,os.walk遍历, @see: help(os) @authoer: U{119<mailto:119@gmail.com>} @version: $id$ ''' import sys import os def get_top_three(path): '''获取给定路径中文件大小最大的三个 @param path: 指定路径 @return 返回一个list,每项为(size, filename) ''' all_file = {} for root, dirs, files in os.walk(path): #遍历path for onefile in files: fname = os.path.join(root, onefile) #获得当前处理文件的完整名字 #print 'fname is %s' %fname fsize = os.stat(fname).st_size #获得当前处理文件大小 #print 'all_file is %s' %all_file if all_file.has_key(fsize): #按照文件大小存储 all_file[fsize].append(fname) else: all_file[fsize] = [fname] fsize_key = all_file.keys() #得到所有的文件大小 print 'fsize_key is %s\n' %fsize_key fsize_key.sort() #排序,从小到大 print 'fsize_key is %s\n' %fsize_key result = [] for i in [-1, -2, -3]: #依次取最大的三个 print 'all_file[fsize_key[i]] is %s\n' %all_file[fsize_key[i]] print 'fsize_key[i] is %s\n' %fsize_key[i] for j in all_file[fsize_key[i]]: #保存 result.append((fsize_key[i], j)) print 'result is %s\n' %result return result[:3] #返回前三个 if __name__ == "__main__": if len(sys.argv) == 1: print 'usage:\n\tpython selectbiggestfile.py path' else: print 'sys.argv is %s\n' %sys.argv abs_path = os.path.abspath(sys.argv[1]) #得到绝对路径 print 'abs_path is %s\n' %abs_path if not os.path.isdir(abs_path): #判断所给的路径是否存在 print '%s is not exist' %abs_path else: top = get_top_three(abs_path) print 'top is %s\n' %top for (s, f) in top: print '%s\t -> \t%s' %(f,s)
运行截屏:
利用ConfigParser,将上述题目中产生的结果按照cdays+1-my.ini格式存储到文件cdays+1-result.txt中。
cdays+1-my.ini内容为:
[Number] filesize = somefilesize filename = somefilename
源代码
#!/usr/bin/env python #-*- coding: utf-8 -*- import os import sys from ConfigParser import RawConfigParser from selectbiggestfile import get_top_three '''parseresult.py @note: 利用ConfigParser解析ini格式 @see: 文档参见http://pydoc.org/2.4.1/ConfigParser.html, 其他例子http://effbot.org/librarybook/configparser-example-1.py @authoer: U{119<mailto:119@gmail.com>} @version: $id$ ''' def iniTT(size_file): '''按照.ini的格式,存储size_file ''' cfg = RawConfigParser() print size_file index = 1 for (s, f) in size_file: cfg.add_section("%d" %index) cfg.set("%d" %index, 'Filename', f) cfg.set('%d' %index, 'FileSize', s) index += 1 cfg.write(open('parseresult.txt','w')) if __name__ == '__main__': if len(sys.argv) == 1: print 'usage:\n\tpython parseresult.py path' else: abs_path = os.path.abspath(sys.argv[1]) if not os.path.isdir(abs_path): print '%s is not exist' % abs_path else: iniTT(get_top_three(abs_path))
运行结果
parseresult.txt如下:
相关文章推荐
- python自动优化之路-day2.1
- Python 代码性能优化技巧
- Python性能优化的20条建议
- 优化方法总结续篇:下降单纯形法(downhill simplex) 及python示例代码
- python程序的优化经验
- python:编写行政区域三级菜单(day 1)
- python-day-20
- python构建关键词共现矩阵速度优化
- 30分钟3300%性能提升——python+memcached网页优化小记
- Python性能优化的20条建议
- 使用memory_profiler 与 cProfile ,line_profiler 优化Python程序运行时间和内存消耗实例
- 在python3中,items()进行了优化
- PythonDay13
- python day18(20170414 )
- Python性能优化的20条建议 (转载)
- Python性能优化
- DAY 41 python包报错解决方案 and python包安装慢问题
- 优化Python代码使其加快作用域内的查找
- Python 优化集锦
- 冒泡排序算法及其优化(Python)