# glob 模块:文件模式匹配 In [1]: ```py import glob ``` `glob` 模块提供了方便的文件模式匹配方法。 例如,找到所有以 `.ipynb` 结尾的文件名: In [2]: ```py glob.glob("*.ipynb") ``` Out[2]: ```py ['11.03 json.ipynb', '11.01 pprint.ipynb', '11.02 pickle and cpickle.ipynb', '11.04 glob.ipynb'] ``` `glob` 函数支持三种格式的语法: * `*` 匹配单个或多个字符 * `?` 匹配任意单个字符 * `[]` 匹配指定范围内的字符,如:[0-9]匹配数字。 假设我们要匹配第 09 节所有的 `.ipynb` 文件: In [3]: ```py glob.glob("../09*/*.ipynb") ``` Out[3]: ```py ['../09\. theano/09.05 configuration settings and compiling modes.ipynb', '../09\. theano/09.03 gpu on windows.ipynb', '../09\. theano/09.07 loop with scan.ipynb', '../09\. theano/09.13 modern net on mnist.ipynb', '../09\. theano/09.11 net on mnist.ipynb', '../09\. theano/09.09 logistic regression .ipynb', '../09\. theano/09.10 softmax on mnist.ipynb', '../09\. theano/09.01 introduction and installation.ipynb', '../09\. theano/09.02 theano basics.ipynb', '../09\. theano/09.12 random streams.ipynb', '../09\. theano/09.04 graph structures.ipynb', '../09\. theano/09.14 convolutional net on mnist.ipynb', '../09\. theano/09.08 linear regression.ipynb', '../09\. theano/09.15 tensor module.ipynb', '../09\. theano/09.06 conditions in theano.ipynb'] ``` 匹配数字开头的文件夹名: In [4]: ```py glob.glob("../[0-9]*") ``` Out[4]: ```py ['../04\. scipy', '../02\. python essentials', '../07\. interfacing with other languages', '../11\. useful tools', '../05\. advanced python', '../10\. something interesting', '../03\. numpy', '../06\. matplotlib', '../08\. object-oriented programming', '../01\. python tools', '../09\. theano'] ```