python中的glob库函数


glob 文件名模式匹配,用来判断每个文件是不是符合模式。

1、通配符

星号(*)匹配零个或多个字符

1 import glob
2 for name in glob.glob('dir/*'):
3     print (name)
dir/file.txt
dir/file1.txt
dir/file2.txt
dir/filea.txt
dir/fileb.txt
dir/subdir

2、单个字符通配符

用问号(?)匹配任何单个的字符。

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、字符范围

当需要匹配一个特定的字符,可以使用一个范围

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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM