最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺析python標(biāo)準(zhǔn)庫中的glob

 更新時(shí)間:2020年03月13日 10:11:17   作者:luminousjj  
glob 文件名模式匹配,不用遍歷整個(gè)目錄判斷每個(gè)文件是不是符合。這篇文章主要介紹了python標(biāo)準(zhǔn)庫中的glob的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

 glob 文件名模式匹配,不用遍歷整個(gè)目錄判斷每個(gè)文件是不是符合。

1、通配符

星號(hào)(*)匹配零個(gè)或多個(gè)字符

import glob
for name in glob.glob('dir/*'):
  print (name)

dir/file.txt
dir/file1.txt
dir/file2.txt
dir/filea.txt
dir/fileb.txt
dir/subdir

列出子目錄中的文件,必須在模式中包括子目錄名:

import glob

#用子目錄查詢文件
print ('Named explicitly:')
for name in glob.glob('dir/subdir/*'):
  print ('\t', name)
#用通配符* 代替子目錄名
print ('Named with wildcard:')
for name in glob.glob('dir/*/*'):
  print ('\t', name)

Named explicitly:
    dir/subdir/subfile.txt
Named with wildcard:
    dir/subdir/subfile.txt

2、單個(gè)字符通配符

用問號(hào)(?)匹配任何單個(gè)的字符。

import glob

for name in glob.glob('dir/file?.txt'):
  print (name)

dir/file1.txt
dir/file2.txt
dir/filea.txt
dir/fileb.txt

3、字符范圍

當(dāng)需要匹配一個(gè)特定的字符,可以使用一個(gè)范圍

import glob
for name in glob.glob('dir/*[0-9].*'):
  print (name)

dir/file1.txt
dir/file2.txt

知識(shí)點(diǎn)補(bǔ)充:Python編程:glob模塊進(jìn)行文件名模式匹配

文件準(zhǔn)備

$ mkdir tmp
$ cd tmp
$ touch file1.txt
$ touch file2.txt
$ touch file3.log
$ ls
file1.txt       file2.txt       file3.log

測試

import glob

# 使用零個(gè)或多個(gè)字符通配符 * 
glob.glob("tmp/*.txt")
Out[1]: 
['file1.txt', 'file2.txt']

# 使用單字符通配符 ?
glob.glob("tmp/file?.txt")
Out[2]: 
['file1.txt', 'file2.txt']

# 使用范圍匹配
glob.glob("tmp/file[0-9].txt")
Out[3]: 
['file1.txt', 'file2.txt']

總結(jié)

到此這篇關(guān)于淺析python標(biāo)準(zhǔn)庫中的glob的文章就介紹到這了,更多相關(guān)python標(biāo)準(zhǔn)庫 glob內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

伊川县| 吉安市| 津市市| 和平区| 北安市| 无为县| 南漳县| 会昌县| 大丰市| 偏关县| 新晃| 鄂托克前旗| 临猗县| 平度市| 武安市| 刚察县| 武威市| 江城| 巧家县| 上饶市| 新巴尔虎右旗| 南阳市| 阳泉市| 阳新县| 财经| 那坡县| 宽城| 雷山县| 汉沽区| 樟树市| 盐津县| 奇台县| 肥城市| 碌曲县| 广东省| 且末县| 深州市| 江阴市| 高淳县| 缙云县| 新野县|