zoukankan      html  css  js  c++  java
  • python里有意思的文件查找glob模块

    python标准库之glob介绍

     glob 文件名模式匹配,不用遍历整个目录判断每个文件是不是符合。

    1、通配符

    星号(*)匹配零个或多个字符

    import glob
    for name in glob.glob('dir/*'):
        print (name)
    复制代码
    dir/file.txt
    dir/file1.txt
    dir/file2.txt
    dir/filea.txt
    dir/fileb.txt
    dir/subdir
    复制代码

    列出子目录中的文件,必须在模式中包括子目录名:

    复制代码
    import glob
    
    #用子目录查询文件 print ('Named explicitly:') for name in glob.glob('dir/subdir/*'): print (' ', name) #用通配符* 代替子目录名 print ('Named with wildcard:') for name in glob.glob('dir/*/*'): print (' ', name)
    复制代码
    Named explicitly:
            dir/subdir/subfile.txt
    Named with wildcard:
            dir/subdir/subfile.txt

    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
  • 相关阅读:
    UML序列图总结
    数据库水平切分的实现原理解析
    oracle imp file data
    putty的设置
    run java jar command
    forex website
    forex tables
    ubuntu set defult jdk
    友情连接
    jstl tag
  • 原文地址:https://www.cnblogs.com/zhigu/p/10656534.html
Copyright © 2011-2022 走看看