glob 模块:文件模式匹配

In [1]:

  1. import glob

glob 模块提供了方便的文件模式匹配方法。

例如,找到所有以 .ipynb 结尾的文件名:

In [2]:

  1. glob.glob("*.ipynb")

Out[2]:

  1. ['11.03 json.ipynb',
  2. '11.01 pprint.ipynb',
  3. '11.02 pickle and cpickle.ipynb',
  4. '11.04 glob.ipynb']

glob 函数支持三种格式的语法:

  • * 匹配单个或多个字符
  • ? 匹配任意单个字符
  • [] 匹配指定范围内的字符,如:[0-9]匹配数字。 假设我们要匹配第 09 节所有的 .ipynb 文件:

In [3]:

  1. glob.glob("../09*/*.ipynb")

Out[3]:

  1. ['../09. theano/09.05 configuration settings and compiling modes.ipynb',
  2. '../09. theano/09.03 gpu on windows.ipynb',
  3. '../09. theano/09.07 loop with scan.ipynb',
  4. '../09. theano/09.13 modern net on mnist.ipynb',
  5. '../09. theano/09.11 net on mnist.ipynb',
  6. '../09. theano/09.09 logistic regression .ipynb',
  7. '../09. theano/09.10 softmax on mnist.ipynb',
  8. '../09. theano/09.01 introduction and installation.ipynb',
  9. '../09. theano/09.02 theano basics.ipynb',
  10. '../09. theano/09.12 random streams.ipynb',
  11. '../09. theano/09.04 graph structures.ipynb',
  12. '../09. theano/09.14 convolutional net on mnist.ipynb',
  13. '../09. theano/09.08 linear regression.ipynb',
  14. '../09. theano/09.15 tensor module.ipynb',
  15. '../09. theano/09.06 conditions in theano.ipynb']

匹配数字开头的文件夹名:

In [4]:

  1. glob.glob("../[0-9]*")

Out[4]:

  1. ['../04. scipy',
  2. '../02. python essentials',
  3. '../07. interfacing with other languages',
  4. '../11. useful tools',
  5. '../05. advanced python',
  6. '../10. something interesting',
  7. '../03. numpy',
  8. '../06. matplotlib',
  9. '../08. object-oriented programming',
  10. '../01. python tools',
  11. '../09. theano']

原文: https://nbviewer.jupyter.org/github/lijin-THU/notes-python/blob/master/11-useful-tools/11.04-glob.ipynb