python - 通配符

通配符:
*: 所有
?: 代表单个字符
.: 当前目录
…:当前目录的上一级目录
[0-9]: 单个字符为0~9
[a-z]:单个字符为a~z
[A-Z]:单个字符为A~Z
[A-Za-z]:A~Z a~z
[0-9A-Za-Z]0~9 A~Z a~z

[[:digit:]]
[[:upper:]]
[[:lower:]]
[[:space:]]

glob官方解读

>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']
>>> glob.glob('**/*.txt', recursive=True)
['2.txt', 'sub/3.txt']
>>> glob.glob('./**/', recursive=True)
['./', './sub/']

>>> import glob
>>> glob.glob('*.gif')
['card.gif']
>>> glob.glob('.c*')
['.card.gif']

举例:
在当前目录下起一个以conf结尾的文档
在这里插入图片描述

import os
import glob

files1 = [file for file in os.listdir('.') if file.endswith('.conf')]
# 获取当前目录所有以.conf结尾的文件;
files2=  glob.glob('./*.conf')
print(files1)
print(files2)

输出:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43067754/article/details/87282852