自己写的第一个python目录及文件查找下程序
来源:互联网 发布:孤岛危机3优化怎么样 编辑:程序博客网 时间:2024/06/04 20:03
功能:查找包含关键字的文件。
注:输入若干个参数,其中,最后一个参数为要查找的目录,默认为当前目录。其余为所要查找的关键字列表。程序稍作修改即可查询所有文件。python不能递归太多,想办法对递归做优化。
当要进行深度递归时,出现错误,如:递归opendaylight文件夹。以后学习os.walk()和os.path.walk()。
#! usr/bin/env python# -*- coding:utf-8 -*-import osimport sysclass Search_file(object):def __init__(self, args = os.path.abspath('.')):self.args = argsdef search_file(self, all_file , dir_path, sub_dir):#print sub_dirfor file in all_file:for x in os.listdir(dir_path):try:path = os.path.join(dir_path, x)if os.path.isfile(path):if 0<=x.find(file):sub_dir1 = os.path.join(sub_dir, x)print sub_dir1except WindowsError, e:print 'no authority for this file',sub_dir1def search_dir_file(self, file, dir_path = os.path.abspath('.'), sub_dir = ''):self.search_file(file, dir_path, sub_dir)for x in os.listdir(dir_path):try:path = os.path.join(dir_path, x)if os.path.isdir(path):sub_dir = os.path.join(sub_dir, x)#print 'diretory:',sub_dirself.search_dir_file(file, path, sub_dir)except WindowsError, e:print 'no authority for this folder:',pathreturn args = sys.argvargs.pop(0)if len(args)==0:print 'hello, you must enter a file name which you want to search!'elif len(args)>=2:if os.path.isdir(args[-1]):dire = args.pop(-1)files = Search_file()files.search_dir_file(args, dire)else:files = Search_file()files.search_dir_file(args)
2014/9/4 19:51 针对系统的文件夹及文件的权限访问异常问题,加入了异常机制。
最后附上针对尾递归优化的文章:http://www.csharpwin.com/csharpspace/12293r3050.shtml
0 0
- 自己写的第一个python目录及文件查找下程序
- 第一个自己写的python程序:windows下备份文件
- 自己写的一个LINUX 下目录文件遍历程序
- 第一个自己写的小程序
- 恶作剧小程序,自己写的第一个autoit程序
- 我的第一个完全自己写的C程序
- 自己写的第一个手机程序播放器Solomon
- 用dev-c++写自己的第一个opengl程序
- 自己写的第一个php小程序
- WordCount------自己写的第一个map/reduce程序------
- 自己写的第一个小程序,记录点滴
- 学c语言第一个自己写的计算器程序
- Python 写的第一个算法程序,果然很强大
- 用 Python+Django 写的第一个 Hello World 程序
- 使用Python写的第一个网络爬虫程序
- linux下查找目录及查找文件
- eclipse+pydev写第一个python程序
- eclipse+pydev写第一个python程序
- cuda glsl opengl 等等之间的关系
- 图解:如何利用win7旗舰版自带的IIS来搭建FTP
- (错误检查)之将某年的第几天转化为某月某日
- 复制构造函数
- spring事务回滚失效的问题
- 自己写的第一个python目录及文件查找下程序
- POJ Drying
- 操作系统
- mtk nand flash配置支持
- JAVA并发编程学习笔记之CAS操作
- 模拟shell ( 进程函数:fork(),execvp(),wait() )
- 二进制输入或文件的读取
- poj2778 DNA Sequence AC自动机+矩阵快速幂
- UVALive 6432 Influence 搜索 剪枝大法好