[python] glob.glob 모듈을 사용하여 하위 폴더를 검색하려면 어떻게해야합니까?

폴더에서 일련의 하위 폴더를 열고 일부 텍스트 파일을 찾고 텍스트 파일의 일부 줄을 인쇄하고 싶습니다. 나는 이것을 사용하고있다 :

configfiles = glob.glob('C:/Users/sam/Desktop/file1/*.txt')

그러나 이것은 하위 폴더에도 액세스 할 수 없습니다. 누구든지 동일한 명령을 사용하여 하위 폴더에 액세스하는 방법을 알고 있습니까?



답변

Python 3.5 이상에서는 새로운 재귀 **/기능을 사용합니다 .

configfiles = glob.glob('C:/Users/sam/Desktop/file1/**/*.txt', recursive=True)

recursive가 설정 되면 **경로 구분 기호가 0 개 이상의 하위 디렉토리와 일치합니다.

이전 Python 버전에서는 glob.glob()하위 디렉토리의 파일을 재귀 적으로 나열 할 수 없습니다.

이 경우 대신 다음 os.walk()과 같이 사용 fnmatch.filter()합니다.

import os
import fnmatch

path = 'C:/Users/sam/Desktop/file1'

configfiles = [os.path.join(dirpath, f)
    for dirpath, dirnames, files in os.walk(path)
    for f in fnmatch.filter(files, '*.txt')]

이것은 디렉토리를 재귀 적으로 걷고 모든 절대 경로 이름을 일치하는 .txt파일로 반환 합니다. 이 특정 경우에는 fnmatch.filter()과잉 일 수 있으며 .endswith()테스트를 사용할 수도 있습니다 .

import os

path = 'C:/Users/sam/Desktop/file1'

configfiles = [os.path.join(dirpath, f)
    for dirpath, dirnames, files in os.walk(path)
    for f in files if f.endswith('.txt')]


답변

바로 아래의 하위 디렉터리에서 파일을 찾으려면 :

configfiles = glob.glob(r'C:\Users\sam\Desktop\*\*.txt')

모든 하위 디렉토리를 순회하는 재귀 버전의 경우 Python 3.5부터 사용 **하고 전달할 수 있습니다 .recursive=True

configfiles = glob.glob(r'C:\Users\sam\Desktop\**\*.txt', recursive=True)

두 함수 호출 모두 목록을 반환합니다. glob.iglob()경로를 하나씩 반환 하는 데 사용할 수 있습니다 . 또는 다음을 사용하십시오pathlib .

from pathlib import Path

path = Path(r'C:\Users\sam\Desktop')
txt_files_only_subdirs = path.glob('*/*.txt')
txt_files_all_recursively = path.rglob('*.txt') # including the current dir

두 메서드 모두 반복자를 반환합니다 (경로를 하나씩 가져올 수 있음).


답변

이 주제에 대해 많은 혼란이 있습니다. 내가 그것을 명확히 할 수 있는지 보자 (Python 3.7) :

  1. glob.glob('*.txt') :현재 디렉토리에서 ‘.txt’로 끝나는 모든 파일과 일치합니다.
  2. glob.glob('*/*.txt') :1과 동일
  3. glob.glob('**/*.txt') :‘.txt’로 끝나는 모든 파일은 바로 아래 하위 디렉토리에서만 일치 하지만 현재 디렉토리에서는 일치하지 않습니다.
  4. glob.glob('*.txt',recursive=True) :1과 동일
  5. glob.glob('*/*.txt',recursive=True) :3과 동일
  6. glob.glob('**/*.txt',recursive=True):현재 디렉토리 및 모든 하위 디렉토리에서 ‘.txt’로 끝나는 모든 파일과 일치합니다.

따라서 항상 지정하는 것이 가장 좋습니다. recursive=True.


답변

glob2의 패키지는 와일드 카드를 지원하며 합리적으로 빠르게

code = '''
import glob2
glob2.glob("files/*/**")
'''
timeit.timeit(code, number=1)

내 랩톱에서는 60,000 개 이상의 파일 경로 를 일치시키는 데 약 2 초가 걸립니다 .


답변

Python 2.6에서 Formic 을 사용할 수 있습니다.

import formic
fileset = formic.FileSet(include="**/*.txt", directory="C:/Users/sam/Desktop/")

공개-나는이 패키지의 작성자입니다.


답변

여기서 수있는 적합한 버전 glob.glob없이 같은 기능은 glob2.

def find_files(directory, pattern='*'):
    if not os.path.exists(directory):
        raise ValueError("Directory not found {}".format(directory))

    matches = []
    for root, dirnames, filenames in os.walk(directory):
        for filename in filenames:
            full_path = os.path.join(root, filename)
            if fnmatch.filter([full_path], pattern):
                matches.append(os.path.join(root, filename))
    return matches

따라서 다음과 같은 dir 구조가 있다면

tests/files
├── a0
   ├── a0.txt
   ├── a0.yaml
   └── b0
       ├── b0.yaml
       └── b00.yaml
└── a1

다음과 같이 할 수 있습니다.

files = utils.find_files('tests/files','**/b0/b*.yaml')
> ['tests/files/a0/b0/b0.yaml', 'tests/files/a0/b0/b00.yaml']

fnmatch파일 이름 만이 아니라 전체 파일 이름 자체에 대해 거의 패턴이 일치합니다.


답변

configfiles = glob.glob('C:/Users/sam/Desktop/**/*.txt")

모든 경우에 작동하지 않고 대신 glob2를 사용합니다.

configfiles = glob2.glob('C:/Users/sam/Desktop/**/*.txt")