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