美文网首页
python 遍历目录

python 遍历目录

作者: momo1023 | 来源:发表于2019-11-28 16:56 被阅读0次

    递归遍历所有目录

    import os
    
    dir_addr = './nlp-stopwords'
    
    def walk_dir(dir_addr):
        '''
        遍历列出当前目录下的所有的文件
        root_dir: 根目录
        subdirs: 子目录
        files: 文件列表
        '''
        for root_dir, subdirs, files in os.walk(dir_addr):
            print(root_dir, subdirs, files)
    
    walk_dir(dir_addr)
    

    输出:

    /Users/momo/Documents/code/nlp-stopwords ['.git'] ['baidu_stopwords.txt', 'common_stopwords.txt', 'SCU_stopwords.txt', 'README.md', 'HIT_stopwords.txt']
    /Users/momo/Documents/code/nlp-stopwords/.git ['objects', 'info', 'logs', 'hooks', 'refs'] ['config', 'HEAD', 'description', 'index', 'packed-refs', 'COMMIT_EDITMSG']
    /Users/momo/Documents/code/nlp-stopwords/.git/objects ['58', 'bb', 'pack', '42', '7b', 'info', 'd2', '14'] []
    /Users/momo/Documents/code/nlp-stopwords/.git/objects/58 [] ['ea97fd757e6a0ddb4420ea45e4d4d859605957']
    /Users/momo/Documents/code/nlp-stopwords/.git/objects/bb [] ['47a8b01a9a940d299154713b2911ee02755b4a']
    /Users/momo/Documents/code/nlp-stopwords/.git/objects/pack [] ['pack-4e2fbdec7a023665bf89864c72f96bdc9ddff3b2.idx', 'pack-4e2fbdec7a023665bf89864c72f96bdc9ddff3b2.pack']
    /Users/momo/Documents/code/nlp-stopwords/.git/objects/42 [] ['10f2808a943567fa5e8fc41eeafba7e9c3bbe0']
    /Users/momo/Documents/code/nlp-stopwords/.git/objects/7b [] ['f86ad3023db60f9452ba4ca6c2eb08ca276e1e']
    /Users/momo/Documents/code/nlp-stopwords/.git/objects/info [] []
    /Users/momo/Documents/code/nlp-stopwords/.git/objects/d2 [] ['f08c173eac503b713e00a14a2a1f9d7c00c0f9']
    /Users/momo/Documents/code/nlp-stopwords/.git/objects/14 [] ['eb732aacbc1b3196b092da09a2c1a126d7b8c1']
    /Users/momo/Documents/code/nlp-stopwords/.git/info [] ['exclude']
    /Users/momo/Documents/code/nlp-stopwords/.git/logs ['refs'] ['HEAD']
    /Users/momo/Documents/code/nlp-stopwords/.git/logs/refs ['heads', 'remotes'] []
    /Users/momo/Documents/code/nlp-stopwords/.git/logs/refs/heads [] ['master']
    /Users/momo/Documents/code/nlp-stopwords/.git/logs/refs/remotes ['origin'] []
    /Users/momo/Documents/code/nlp-stopwords/.git/logs/refs/remotes/origin [] ['HEAD', 'master']
    /Users/momo/Documents/code/nlp-stopwords/.git/hooks [] ['commit-msg.sample', 'pre-rebase.sample', 'pre-commit.sample', 'applypatch-msg.sample', 'fsmonitor-watchman.sample', 'pre-receive.sample', 'prepare-commit-msg.sample', 'post-update.sample', 'pre-applypatch.sample', 'pre-push.sample', 'update.sample']
    /Users/momo/Documents/code/nlp-stopwords/.git/refs ['heads', 'tags', 'remotes'] []
    /Users/momo/Documents/code/nlp-stopwords/.git/refs/heads [] ['master']
    /Users/momo/Documents/code/nlp-stopwords/.git/refs/tags [] []
    /Users/momo/Documents/code/nlp-stopwords/.git/refs/remotes ['origin'] []
    /Users/momo/Documents/code/nlp-stopwords/.git/refs/remotes/origin [] ['HEAD', 'master']
    

    仅遍历当前目录

    def listdir(dir_addr):
        filenames = os.listdir(dir_addr)
        print(filenames)
    

    输出:

    ['baidu_stopwords.txt', 'common_stopwords.txt', 'SCU_stopwords.txt', 'README.md', '.git', 'HIT_stopwords.txt']
    

    文件名匹配

    import os
    import re
    
    dir_addr = './nlp-stopwords'
    
    filenames = listdir(dir_addr)
    
    for item in filenames:
    #     match = re.match('(.*)txt(.*?)', item)
    #     match = re.search('.txt', item)
    #     match = re.match('\.', item)
        match = re.match('(.*).txt', item)
        if match:
            print(item)
    

    输出:

    baidu_stopwords.txt
    common_stopwords.txt
    SCU_stopwords.txt
    HIT_stopwords.txt
    

    相关文章

      网友评论

          本文标题:python 遍历目录

          本文链接:https://www.haomeiwen.com/subject/lfwbwctx.html