參考:
http://python.jobbole.com/81552/:Python模块学习:glob文件路径查找
http://blog.csdn.net/suiyunonghen/article/details/4517103:python中的一个好用的文件名称操作模块glob
https://docs.python.org/2/library/glob.html:10.7. glob - Unix style pathname pattern expansion
https://docs.python.org/2/library/fnmatch.html#module-fnmatch:10.8. fnmatch - Unix filename pattern matching
#################################################################3
測试集文件分布情况:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
3##############################################################3
本人glob源代码位置:/usr/lib/python2.7/glob.py
函数名:glob
參数名:pathname - 路径名(能够使用相对路径或绝对路径)
功能:返回一个列表。存储全部匹配你參数名的文件路径(当你使用相对路径时,文件路径为相对路径;当你使用绝对路径时,文件路径为绝对路径)
note:查找的范围仅在路径名所在文件夹
路径名必须使用通配符。只是仅仅有三种(*/?/[])。*代表全部文件,?代表一个字符,[]表示想要匹配的多个字符(比方。[0-9]表示匹配数字0-9)
查找当前文件夹下全部文件和文件夹:
note:由上图可知,仅提供路径并不会得到不论什么结果
查找当前文件夹下的txt文件:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
已知測试集共同拥有三层
查找第二层的全部txt文件:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
函数iglob(pathname)功能和glob类似,仅仅只是iglob返回的是迭代器。查看源代码可知。两个函数事实上一模一样
def glob(pathname):
"""Return a list of paths matching a pathname pattern. The pattern may contain simple shell-style wildcards a la
fnmatch. However, unlike fnmatch, filenames starting with a
dot are special cases that are not matched by '*' and '?'
patterns. """
return list(iglob(pathname))
###########################################################################33
本人fnmatch源代码位置:/usr/lib/python2.7/fnmatch.py
共同拥有四个函数:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
函数filter:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
返回列表names中符合模式pat的子集
函数fnmatch
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
測试name是否符合pat的模式
pat格式符合Unix文本类型:
*-表示全部
?-表示一个单一字符
[seq]-表示符合[]内的序列的字符
[!seq]-表示不符合[]内的序列的字符
查看源代码知,该函数功能和fnmatcncase一样:
def fnmatch(name, pat):
"""Test whether FILENAME matches PATTERN. Patterns are Unix shell style: * matches everything
? matches any single character
[seq] matches any character in seq
[!seq] matches any char not in seq An initial period in FILENAME is not special.
Both FILENAME and PATTERN are first case-normalized
if the operating system requires it.
If you don't want this, use fnmatchcase(FILENAME, PATTERN).
""" import os
name = os.path.normcase(name)
pat = os.path.normcase(pat)
return fnmatchcase(name, pat)
函数fnmatchcase
没有规范化參数。只推断文件名称是否匹配模式符。包含大写和小写
note:參数均为单个字符串