|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# glob 模块:文件模式匹配" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 1, |
| 13 | + "metadata": { |
| 14 | + "collapsed": false |
| 15 | + }, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "import glob" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "markdown", |
| 23 | + "metadata": {}, |
| 24 | + "source": [ |
| 25 | + "`glob` 模块提供了方便的文件模式匹配方法。\n", |
| 26 | + "\n", |
| 27 | + "例如,找到所有以 `.ipynb` 结尾的文件名:" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": 2, |
| 33 | + "metadata": { |
| 34 | + "collapsed": false |
| 35 | + }, |
| 36 | + "outputs": [ |
| 37 | + { |
| 38 | + "data": { |
| 39 | + "text/plain": [ |
| 40 | + "['11.03 json.ipynb',\n", |
| 41 | + " '11.01 pprint.ipynb',\n", |
| 42 | + " '11.02 pickle and cpickle.ipynb',\n", |
| 43 | + " '11.04 glob.ipynb']" |
| 44 | + ] |
| 45 | + }, |
| 46 | + "execution_count": 2, |
| 47 | + "metadata": {}, |
| 48 | + "output_type": "execute_result" |
| 49 | + } |
| 50 | + ], |
| 51 | + "source": [ |
| 52 | + "glob.glob(\"*.ipynb\")" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "markdown", |
| 57 | + "metadata": {}, |
| 58 | + "source": [ |
| 59 | + "`glob` 函数支持三种格式的语法:\n", |
| 60 | + "\n", |
| 61 | + "- `*` 匹配单个或多个字符\n", |
| 62 | + "- `?` 匹配任意单个字符\n", |
| 63 | + "- `[]` 匹配指定范围内的字符,如:[0-9]匹配数字。\n", |
| 64 | + "\n", |
| 65 | + "假设我们要匹配第 09 节所有的 `.ipynb` 文件:" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": 3, |
| 71 | + "metadata": { |
| 72 | + "collapsed": false |
| 73 | + }, |
| 74 | + "outputs": [ |
| 75 | + { |
| 76 | + "data": { |
| 77 | + "text/plain": [ |
| 78 | + "['../09. theano/09.05 configuration settings and compiling modes.ipynb',\n", |
| 79 | + " '../09. theano/09.03 gpu on windows.ipynb',\n", |
| 80 | + " '../09. theano/09.07 loop with scan.ipynb',\n", |
| 81 | + " '../09. theano/09.13 modern net on mnist.ipynb',\n", |
| 82 | + " '../09. theano/09.11 net on mnist.ipynb',\n", |
| 83 | + " '../09. theano/09.09 logistic regression .ipynb',\n", |
| 84 | + " '../09. theano/09.10 softmax on mnist.ipynb',\n", |
| 85 | + " '../09. theano/09.01 introduction and installation.ipynb',\n", |
| 86 | + " '../09. theano/09.02 theano basics.ipynb',\n", |
| 87 | + " '../09. theano/09.12 random streams.ipynb',\n", |
| 88 | + " '../09. theano/09.04 graph structures.ipynb',\n", |
| 89 | + " '../09. theano/09.14 convolutional net on mnist.ipynb',\n", |
| 90 | + " '../09. theano/09.08 linear regression.ipynb',\n", |
| 91 | + " '../09. theano/09.15 tensor module.ipynb',\n", |
| 92 | + " '../09. theano/09.06 conditions in theano.ipynb']" |
| 93 | + ] |
| 94 | + }, |
| 95 | + "execution_count": 3, |
| 96 | + "metadata": {}, |
| 97 | + "output_type": "execute_result" |
| 98 | + } |
| 99 | + ], |
| 100 | + "source": [ |
| 101 | + "glob.glob(\"../09*/*.ipynb\")" |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "markdown", |
| 106 | + "metadata": {}, |
| 107 | + "source": [ |
| 108 | + "匹配数字开头的文件夹名:" |
| 109 | + ] |
| 110 | + }, |
| 111 | + { |
| 112 | + "cell_type": "code", |
| 113 | + "execution_count": 4, |
| 114 | + "metadata": { |
| 115 | + "collapsed": false |
| 116 | + }, |
| 117 | + "outputs": [ |
| 118 | + { |
| 119 | + "data": { |
| 120 | + "text/plain": [ |
| 121 | + "['../04. scipy',\n", |
| 122 | + " '../02. python essentials',\n", |
| 123 | + " '../07. interfacing with other languages',\n", |
| 124 | + " '../11. useful tools',\n", |
| 125 | + " '../05. advanced python',\n", |
| 126 | + " '../10. something interesting',\n", |
| 127 | + " '../03. numpy',\n", |
| 128 | + " '../06. matplotlib',\n", |
| 129 | + " '../08. object-oriented programming',\n", |
| 130 | + " '../01. python tools',\n", |
| 131 | + " '../09. theano']" |
| 132 | + ] |
| 133 | + }, |
| 134 | + "execution_count": 4, |
| 135 | + "metadata": {}, |
| 136 | + "output_type": "execute_result" |
| 137 | + } |
| 138 | + ], |
| 139 | + "source": [ |
| 140 | + "glob.glob(\"../[0-9]*\")" |
| 141 | + ] |
| 142 | + } |
| 143 | + ], |
| 144 | + "metadata": { |
| 145 | + "kernelspec": { |
| 146 | + "display_name": "Python 2", |
| 147 | + "language": "python", |
| 148 | + "name": "python2" |
| 149 | + }, |
| 150 | + "language_info": { |
| 151 | + "codemirror_mode": { |
| 152 | + "name": "ipython", |
| 153 | + "version": 2 |
| 154 | + }, |
| 155 | + "file_extension": ".py", |
| 156 | + "mimetype": "text/x-python", |
| 157 | + "name": "python", |
| 158 | + "nbconvert_exporter": "python", |
| 159 | + "pygments_lexer": "ipython2", |
| 160 | + "version": "2.7.6" |
| 161 | + } |
| 162 | + }, |
| 163 | + "nbformat": 4, |
| 164 | + "nbformat_minor": 0 |
| 165 | +} |
0 commit comments