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

相关文章:

  • 2021-07-12
  • 2022-12-23
  • 2021-11-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-13
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-21
  • 2022-12-23
  • 2021-12-22
  • 2021-10-28
  • 2022-02-22
  • 2022-12-23
相关资源
相似解决方案