日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

基于py36的glob模块总结

發(fā)布時(shí)間:2025/4/5 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于py36的glob模块总结 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

glob介紹

glob是python自帶的一個(gè)操作文件的相關(guān)模塊。用它可以查找符合特定規(guī)則的文件路徑名。使用該模塊查找文件,只需要用到: “*”, “?”, “[]”, “**”這四個(gè)匹配符;

* : 匹配所有字符 ? : 匹配一個(gè)字符 [] : 匹配指定范圍內(nèi)的字符,如[0-9]匹配數(shù)字。 ** : 匹配所有文件、目錄、子目錄和子目錄里的文件(需配合其他參數(shù))

下面實(shí)例的目錄結(jié)構(gòu)

glob.glob

函數(shù)分析

glob.glob(pathname, *, recursive=False)

pathname : 要匹配的文件名稱* :非參數(shù),只是限定符recursive : If recursive is true, the pattern '**' will match any files and zero or more directories and subdirectories. 默認(rèn)為False如果有匹配,glob.glob(path)的結(jié)果放入一個(gè)列表中返回如果沒有匹配的,glob.glob(path)將返回一個(gè)空的list:[]

實(shí)例驗(yàn)證

  • 對(duì)于recursive和**的實(shí)例驗(yàn)證
  • res = glob.glob(’.\imgs\**’, recursive=False)
    print(res)
    [’.\imgs\cat.png’, ‘.\imgs\hello0.txt’, ‘.\imgs\hello100.txt’, ‘.\imgs\hello2.txt’, ‘.\imgs\one_dir’]

    res = glob.glob(’.\imgs\**’, recursive=True)
    print(res)
    [’.\imgs\’, ‘.\imgs\cat.png’, ‘.\imgs\hello0.txt’, ‘.\imgs\hello100.txt’, ‘.\imgs\hello2.txt’, ‘.\imgs\one_dir’, ‘.\imgs\one_dir\one_file.py’, ‘.\imgs\one_dir\one_file.txt’]

    當(dāng)**匹配符遇到recursive=True,擦出了不一樣的火花

  • 對(duì)于*號(hào)的實(shí)例驗(yàn)證
    • 匹配imgs路徑下的所有文件/路徑
      res = glob.glob(’.\imgs\*’, recursive=True)
      print(res)
      [’.\imgs\cat.png’, ‘.\imgs\hello0.txt’, ‘.\imgs\hello100.txt’, ‘.\imgs\hello2.txt’, ‘.\imgs\one_dir’]

      res = glob.glob(’.\imgs\*’, recursive=False)
      print(res)
      [’.\imgs\cat.png’, ‘.\imgs\hello0.txt’, ‘.\imgs\hello100.txt’, ‘.\imgs\hello2.txt’, ‘.\imgs\one_dir’]

      recursive無論為True還是False 對(duì)于*匹配符都是沒有影響的。

    • 只匹配目錄下的所有文件,不再匹配目錄下的目錄
      res = glob.glob(’.\imgs\*.*’)
      print(res)
      [’.\imgs\cat.png’, ‘.\imgs\hello0.txt’, ‘.\imgs\hello100.txt’, ‘.\imgs\hello2.txt’]

    • 過濾出所有txt文件
      res = glob.glob(’.\imgs\*.txt’, recursive=False)
      print(res)
      [’.\imgs\hello0.txt’, ‘.\imgs\hello100.txt’, ‘.\imgs\hello2.txt’]

    • 匹配當(dāng)前路徑下面的路徑下的所有png圖片
      res = glob.glob(’.\*\*.png’)
      print(res)
      [’.\imgs\cat.png’]

  • 對(duì)于?號(hào)的使用
    • 只匹配hello0.txt到hello9.txt
      res = glob.glob(’.\imgs\hello?.txt’)
      print(res)
      [’.\imgs\hello0.txt’, ‘.\imgs\hello2.txt’]

    • 獲取目錄下文件名為6個(gè)字符的文件
      res = glob.glob(’.\imgs\??????.*’)
      print(res)
      [’.\imgs\hello0.txt’, ‘.\imgs\hello2.txt’]

  • 對(duì)于[]號(hào)的使用
    • 只匹配hello0.txt到hello9.txt
      res = glob.glob(’.\imgs\hello[0-9].txt’)
      print(res)
      [’.\imgs\hello0.txt’, ‘.\imgs\hello2.txt’]

    • 匹配imgs目錄下包含數(shù)字的文件
      res = glob.glob(’.\imgs\*[0-9]*.*’)
      print(res)
      [’.\imgs\hello0.txt’, ‘.\imgs\hello100.txt’, ‘.\imgs\hello2.txt’]

  • 匹配以.開頭的文件或路徑
  • glob函數(shù)默認(rèn)不搜索以.點(diǎn)號(hào)開頭的文件和路徑,如果要求的話需要單獨(dú)寫個(gè)點(diǎn)號(hào).
    res = glob.glob(’.\imgs\.*.*’)
    print(res)
    [’.\imgs\.pip.config’]

    glob.iglob函數(shù)分析

    iglob(pathname, *, recursive=False)
    iglob和glob.glob的用法一樣。區(qū)別就在于它返回的是一個(gè)生成器。

    res = glob.iglob(’.\imgs\*[0-9]*.*’)
    print(res)
    <generator object _iglob at 0x000001F508DA4678>

    for r in res:
    print(r)
    .\imgs\hello0.txt
    .\imgs\hello100.txt
    .\imgs\hello2.txt

    glob.escape函數(shù)分析

    escape(pathname)
    輸入路徑名稱,轉(zhuǎn)義所有特殊字符
    它返回的是一個(gè)glob規(guī)則,將 *,?,[] 這三個(gè)特殊符號(hào)轉(zhuǎn)換成可以去匹配含有這三個(gè)特殊符號(hào)字符串的glob規(guī)則

    res = glob.escape(’.\imgs\*.txt’)
    print(res)
    .\imgs[*].txt

    總結(jié)

    以上是生活随笔為你收集整理的基于py36的glob模块总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。