python glob model 柔情只为你懂 2021-11-26 13:08 199阅读 0赞 **简介** -------------------- glob是python自己带的一个文件操作相关模块,用它可以查找符合自己的目的文件,就类似于Windows下的文件搜索。支持通配符:\*,?,\[ \] 这三个通配符。 * \* 代表0个或多个字符 * ? 代表一个字符 * \[ \] 匹配指定范围内的字符,如\[0-9\]匹配数组 **glob方法** -------------------- 它的主要方法是 glob,该方法返回所有匹配的文件路径列表。 * @ 该方法需要一个参数:指定匹配的路径字符串(可以是绝对路径或相对路径) * @ 返回为当前目录里的文件路径列表,不包括子文件夹里的文件 Note: 如果传递的参数使用相对路径,则返回值也使用相对路径 **glob 示例** -------------------- 获取C盘下的所有txt文件 <table> <tbody> <tr> <td> <div> 1 </div></td> <td> <div> <div> <code>glob.glob(r</code> <code>'c:\*.txt'</code> <code>)</code> </div> </div></td> </tr> </tbody> </table> 获取目录pic下的所有jpg文件 <table> <tbody> <tr> <td> <div> 1 </div></td> <td> <div> <div> <code>glob.glob(r</code> <code>'E:\pic\*\*.jpg'</code> <code>)</code> </div> </div></td> </tr> </tbody> </table> 使用相对路径的例子 <table> <tbody> <tr> <td> <div> 1 </div></td> <td> <div> <div> <code>glob.glob(r</code> <code>'../*.py'</code> <code>)</code> </div> </div></td> </tr> </tbody> </table> **iglob方法** -------------------- 获取一个可遍历对象,使用它可以逐个获取匹配的文件路径名。 与glob.glob的区别是:glob.glob同时获取所有的匹配路径,而glob.iglob一次只获取一个匹配路径。 示例: <table> <tbody> <tr> <td> <div> 1 </div> <div> 2 </div> <div> 3 </div> <div> 4 </div></td> <td> <div> <div> <code>f </code> <code>=</code> <code>glob.iglob(r</code> <code>'../*.py'</code> <code>) </code> </div> <div> <code>print</code> <code>f </code> <code>#<generator object iglob at 0x00B9FF80> </code> </div> <div> <code>for</code> <code>py </code> <code>in</code> <code>f: </code> </div> <div> <code> </code> <code>print</code> <code>py</code> </div> </div></td> </tr> </tbody> </table> 官方说明: 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/Python1.5/Makefile) or relative (like http://www.cnblogs.com/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.gif, 2.txt, andcard.gif. glob() will produce the following results. Notice how any leading components of the path are preserved. <table> <tbody> <tr> <td> <div> 1 </div> <div> 2 </div> <div> 3 </div> <div> 4 </div> <div> 5 </div> <div> 6 </div> <div> 7 </div> <div> 8 </div> <div> 9 </div> <div> 10 </div> <div> 11 </div></td> <td> <div> <div> <code>>>> </code> <code>import</code> <code>glob </code> </div> <div> <code> </code> </div> <div> <code>>>> glob.glob(</code> <code>'./[0-9].*'</code> <code>) </code> </div> <div></div> <div> <code>[</code> <code>'./1.gif'</code> <code>, </code> <code>'./2.txt'</code> <code>] </code> </div> <div> <code>>>> glob.glob(</code> <code>'*.gif'</code> <code>) </code> </div> <div> <code> </code> </div> <div> <code>[</code> <code>'1.gif'</code> <code>, </code> <code>'card.gif'</code> <code>] </code> </div> <div> <code>>>> glob.glob(</code> <code>'?.gif'</code> <code>) </code> </div> <div> <code> </code> </div> <div> <code>[</code> <code>'1.gif'</code> <code>]</code> </div> </div></td> </tr> </tbody> </table> [来自为知笔记(Wiz)][Wiz] 转载于:https://www.cnblogs.com/riverlcn/archive/2013/05/23/3094224.html [Wiz]: http://www.wiz.cn/i/434ad8af
还没有评论,来说两句吧...