python - 文件枚举
来源:互联网 发布:nginx 三级域名泛解析 编辑:程序博客网 时间:2024/05/29 09:07
有时候做测试,需要生成文件列表,今天写个小工具。
#!/usr/bin/env pythonimport osimport sysfrom optparse import OptionParserdef getfilepath(path): for dirpath, dirnames, filenames in os.walk(path): for filename in filenames: payload = "%s/%s" % (dirpath, filename) yield payload.replace(path, "/")def getfilelist(path, extension=None, exclude=False): if not os.path.isdir(path): sys.stdout.write("please set a directory path.") else: # right directory path # no extension if extension is None: # no extension and no exclude if exclude is False: for filepath in getfilepath(path): sys.stdout.write(filepath + '\n') # no extension and exclude else: sys.stdout.write('Please set exclude option with extension.\n') # with extension else: # with extension and no exclude if exclude is False: for filepath in getfilepath(path): fileext = filepath.split('.')[-1] if extension.upper() == fileext.upper(): sys.stdout.write(filepath + '\n') # with extension and with exclude else: for filepath in getfilepath(path): fileext = filepath.split('.')[-1] if extension.upper() != fileext.upper(): sys.stdout.write(filepath + '\n')def main(): parser = OptionParser() parser.add_option('-p', '--path', metavar='PATH', help='set path for file list', dest="path") parser.add_option('-e', '--ext', metavar='EXTENSION', help='set extension for filter', dest='ext') parser.add_option('-n', '--exclude', metavar='EXCLUDE', help='exclude the extension set by -e', dest='exclude', action='store_true', default=False) options, args = parser.parse_args() if not options.path: parser.error('select -h for help') else: getfilelist(options.path, options.ext, options.exclude)if __name__ == '__main__': main()
0 0
- python - 文件枚举
- python枚举
- Python枚举
- Python枚举指定文件夹下的特定文件
- 文件枚举
- python 枚举窗体 EnumWindows
- Python枚举类型
- python-enumerate枚举函数
- Python基础-枚举
- Python 的枚举 Enum
- python 错误枚举
- python 枚举类型实现
- Python基础-枚举-Enum
- Python 中的枚举类型
- Python 学习笔记(三):文件,高级特性,枚举,正则表达式,模块
- Linux下枚举文件
- 枚举文件夹中的文件
- vc 枚举文件
- 我的PHP之路--学习笔记(1)
- 向架构师进军-->可重用架构资源
- 异常处理+代码混淆(Crashlytics+proguard)
- Android推送方案研究
- Android Bitmap 全面解析(二)加载多张图片的缓存处理 ...
- python - 文件枚举
- ubuntu 12.04 下nginx安装步骤
- Jquery 动态添加dom后,再为其绑定事件
- Android 语音识别 研究
- Linux内核实现的双向链表(2)
- mybatis源代码分析:mybatis延迟加载机制改进
- hibernate的createQuery语句理解
- SVN 服务器的搭建
- pcm 格式 转换成 wav