본문 바로가기

프로그래밍 관련/[Python] 간단 해결

[Python] glob 정규식으로 폴더 및 파일 제외

- 해결

# glob exclude folder

import glob

f = glob.glob(pathImageRoot/[!_]*)
print(f)

# ['pathImageRoot/image.jpg', 'pathImageRoot/text.txt', 'pathImageRoot/file.exe', ...]


'''
# example simple regular expression

1) /**/* : 현재 디렉토리 및 하위 디렉토리에 존재하는 모든 파일 리스트화
2) /*[txt$|jpg$]: 현재 디렉토리 내에 존재하는 모든 .txt, .jpg 파일 리스트화
3) /text[1-3].txt: 현재 디렉토리 내에 있는 text1.txt, text2.txt 파일 리스트화
'''

 

- 참고 (StackOverflow)

 

glob exclude pattern

I have a directory with a bunch of files inside: eee2314, asd3442 ... and eph. I want to exclude all files that start with eph with the glob function. How can I do it?

stackoverflow.com