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
  • 相关阅读:
    jQuery..1..基本使用..选择
    ORZ各路神犇
    马上搞定Android平台的Wi-Fi Direct开发
    Linux环境下搭建Android开发环境
    笑谈接口回调
    AIDL通信原理
    某个Java面试题
    直接下载SpringBoot项目本地的Excel文件
    用JSP做后台管理系统
    Singleton
  • 原文地址:https://www.cnblogs.com/luminousjj/p/9359543.html
Copyright © 2011-2022 走看看