129、glob 模块:文件模式匹配
本文最后更新于 68 天前,其中的信息可能已经过时,如有错误请发送邮件到wuxianglongblog@163.com

glob 模块:文件模式匹配

import glob

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

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

glob.glob("*.ipynb")
['11.03 json.ipynb',
 '11.01 pprint.ipynb',
 '11.02 pickle and cpickle.ipynb',
 '11.04 glob.ipynb']

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

  • * 匹配单个或多个字符
  • ? 匹配任意单个字符
  • [] 匹配指定范围内的字符,如:[0-9]匹配数字。

假设我们要匹配第 09 节所有的 .ipynb 文件:

glob.glob("../09*/*.ipynb")
['../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']

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

glob.glob("../[0-9]*")
['../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']
谨此笔记,记录过往。凭君阅览,如能收益,莫大奢望。
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇