天天看点

python-基础语法-glob.glob()

0.摘要:

glob是实用的文件名匹配库,glob.glob()函数将会匹配给定路径下的所有pattern,并以列表形式返回。

用它可以查找符合特定规则的文件路径名。查找文件只用到三个匹配符:

”*”, 匹配 0 个或多个字符;

    “?”, ”?”匹配单个字符;

    “[]”:”[]”匹配指定范围内的字符,如:[0-9]匹配数字;

注意:如果文件名以“点”开头 ,无法被 '*' 和 '?'匹配,如:".card.gif"

1. glob方法:

glob模块的主要方法就是glob,该方法返回所有匹配的文件路径列表(list);

该方法需要一个参数用来指定匹配的路径字符串(字符串可以为绝对路径也可以为相对路径),其返回的文件名只包括当前目录里的文件名,不包括子文件夹里的文件。

注意:python路径中分隔符默认为"/",window系统中路径中分隔符为“\”

window系统下 glob.glob()返回的文件路径为"\\",并将“\”转变为“\\”,不会改变路径中的"/"

import glob
import os
ckpt = tf.train.get_checkpoint_state("D:/document/deep learning/FCN.tensorflow-master/logs")
print(ckpt)
#print(ckpt.model_checkpoint_path)
path='D:\document'    #window系统文件路径格式
path1='D:/document'  
#path=os.path.join(path1,'*.png')
#print(path)
date=glob.glob(os.path.join(path,'*.jpg'))
print(date)
date1=glob.glob(os.path.join(path1,'*.jpg'))
print(date1)
           

输出结果

['D:\\document\\image.jpg', 'D:\\document\\image1.jpg', 'D:\\document\\image2.jpg', 'D:\\document\\image3.jpg', 'D:\\document\\image3s.jpg', 'D:\\document\\image4.jpg', 'D:\\document\\imagex.jpg', 'D:\\document\\img.jpg', 'D:\\document\\img2.jpg', 'D:\\document\\img3.jpg', 'D:\\document\\mayun2.jpg']
['D:/document\\image.jpg', 'D:/document\\image1.jpg', 'D:/document\\image2.jpg', 'D:/document\\image3.jpg', 'D:/document\\image3s.jpg', 'D:/document\\image4.jpg', 'D:/document\\imagex.jpg', 'D:/document\\img.jpg', 'D:/document\\img2.jpg', 'D:/document\\img3.jpg', 'D:/document\\mayun2.jpg']