码迷,mamicode.com
首页 > 编程语言 > 详细

python中的glob模块

时间:2019-07-25 16:11:36      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:cas   not   any   file   转义   文件   tin   names   python   

简介

glob() 函数返回匹配指定模式的文件名或目录。 该函数返回一个包含有匹配文件 / 目录的列表。如果出错返回 False。

方法介绍

.glob(pathname, *, recursive=False)

"""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.

    If recursive is true, the pattern ‘**‘ will match any files and
    zero or more directories and subdirectories.
"""
# recursive默认为False,表示只返回该路径下的目录及文件列表;如果为True,则路径后应该跟**,可递归返回该路径下的所有目录及目录下的文件,否则效果跟False一样

glob.glob(r"C:\Users\Desktop\FinanicalBook\.*")   # 返回所有的隐藏目录及文件列表
glob.glob(r"C:\Users\Desktop\FinanicalBook\*")    # 返回FinanicalBook目录下的所有目录及文件列表
glob.glob(r"C:\Users\Desktop\FinanicalBook\a*")   # 返回以a开头的目录及文件列表
glob.glob(r"C:\Users\Desktop\FinanicalBook\*q")   # 返回以q结尾的目录及文件列表
glob.glob(r"C:\Users\Desktop\FinanicalBook\**", recursive=True) # 递归匹配任何文件以及零个或多个目录和子目录

.iglob(pathname, *, recursive=False)

用法跟glob()一样,只不过该函数的返回值是一个generator(生成器)

.escape(pathname)

"""
Escape all special characters.
Escaping is done by wrapping any of "*?[" between square brackets
"""
# 转义所有特殊字符
# 转义是通过在方括号之间包装“*?[”来完成的

# 例:
res = glob.escape(r"C:\Users\Desktop\FinanicalBook\*..\?\[A-Z^$|\res")
# C:\Users\Desktop\FinanicalBook\[*]..\[?]\[[]A-Z^$|\res

  

 

python中的glob模块

标签:cas   not   any   file   转义   文件   tin   names   python   

原文地址:https://www.cnblogs.com/rongge95500/p/11244853.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!