glob是Python自己带的一个文件操作相关模块,用它可以查找符合自己目的的文件,就类似于Windows下的文件搜索,支持通配符操作,*,?,[]这三个通配符,*代表0个或多个字符,?代表一个字符,[]匹配指定范围内的字符,如[0-9]匹配数字。

它的主要方法就是glob,该方法返回所有匹配的文件路径列表,该方法需要一个参数用来指定匹配的路径字符串(本字符串可以为绝对路径也可以为相对路径),其返回的文件名只包括当前目录里的文件名,不包括子文件夹里的文件。

python手机中的介绍:

The glob module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell. No tilde expansion is done, but *,?, and character ranges expressed with [] will be correctly matched. This is done by using the os.listdir() and fnmatch.fnmatch() functions in concert, and not by actually invoking a subshell. (For tilde and shell variable expansion, use os.path.expanduser() and os.path.expandvars().)

glob.glob(pathname) #返回列表
Return a possibly-empty list of path names that match pathname, which must be a string containing a path specification. pathname can be either absolute (like /usr/src/Python-1.5/Makefile) or relative (like ../../Tools/*/*.gif), and can contain shell-style wildcards. Broken symlinks are included in the results (as in the shell).
glob.iglob(pathname) #返回迭代器

Return an iterator which yields the same values as glob() without actually storing them all simultaneously.

New in version 2.5.

For example, consider a directory containing only the following files: 1.gif2.txt, and card.gifglob() will produce the following results. Notice how any leading components of the path are preserved.

>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']

上代码:

  1. import glob
  2. fileList = glob.glob(r'c:\*.txt')
  3. print fileList
  4. for file_name in fileList:
  5. print file_name
  6. print '*'*40
  7. fileGen = glob.iglob(r'c:\*.txt')
  8. print fileGen
  9. for filename in fileGen:
  10. print filename

结果:

    1. ['c:\\1.txt', 'c:\\adf.txt', 'c:\\baidu.txt', 'c:\\resultURL.txt']
    2. c:\1.txt
    3. c:\adf.txt
    4. c:\baidu.txt
    5. c:\resultURL.txt
    6. ****************************************
    7. <generator object iglob at 0x01DC1E90>
    8. c:\1.txt
    9. c:\adf.txt
    10. c:\baidu.txt
    11. c:\resultURL.txt

最新文章

  1. MVC CheckBoxList的实现
  2. 三:关于tcp
  3. 37-wc 简明笔记
  4. SSH整合简述一
  5. 透透彻彻IoC(你没有理由不懂!)
  6. MATLAB中mexFunction函数的接口规范(转载)
  7. UVa 548 (二叉树的递归遍历) Tree
  8. Webform——Repeater多表联合显示
  9. cdoj 383 japan 树状数组
  10. Android中由IP地址查询经纬度坐标的实例
  11. BZOJ 1006: [HNOI2008]神奇的国度( MCS )
  12. 使用unity创建塔防游戏(原译)(part1)
  13. 阅读http://zh.lucida.me/有感
  14. Linux sort -g 的困惑
  15. ssh简单配置
  16. beanstalk 安装
  17. 关于collectionView和tableView的两种cell的出列方法的区别
  18. windows环境下手动安装Mysql8
  19. GC调优
  20. Mysql宽字节注入(转)

热门文章

  1. 一个MySQL JDBC驱动bug引起的血案
  2. golang glog
  3. Fiddler 抓包工具详解
  4. 08 redis缓存穿透、缓存雪崩、缓存击穿
  5. 1 java 笔记
  6. MYSQL AND 和 OR
  7. rabbimq 生产消费者
  8. 《浏览器工作原理与实践》&lt;04&gt;从输入URL到页面展示,这中间发生了什么?
  9. 08_Redis通用命令
  10. 05_Redis_List命令