- glob
It finds all the pathnames matching a specified pattern.
When you need the path or name of files with a specific pattern or extension, it returns a file name that meets the conditions presented by the user in the form of a list.
- '*' : any string of arbitrary length
dir : file1.txt, file2.txt, file101.txt, file102.txt, filea.txt, fileb.txt, file1.jpg, file2.jp
dir/subdir : subfile1.txt, subfile2.txt
import glob
output = glob.glob('dir/*.txt')
>>> print(output)
['dir\\file1.txt', 'dir\\file101.txt', 'dir\\file102.txt', 'dir\\file2.txt', 'dir\\filea.txt', 'dir\\fileb.txt']
- '**' : If recursive is true, the pattern “**” will match any files and zero or more directories, subdirectories and symbolic links to directories.
output = glob.glob('dir/**', recursive=True)
>>> print(output)
['dir\\', 'dir\\file1.bmp', 'dir\\file1.txt', 'dir\\file101.txt', 'dir\\file102.txt', 'dir\\file2.bmp', 'dir\\file2.txt', 'dir\\filea.txt', 'dir\\fileb.txt', 'dir\\subdir', 'dir\\subdir\\subfile1.txt', 'dir\\subdir\\subfile2.txt']
- '?' : a single letter
output = glob.glob('dir/file?.*')
>>> print(output)
['dir\\file1.bmp', 'dir\\file1.txt', 'dir\\file2.bmp', 'dir\\file2.txt', 'dir\\filea.txt', 'dir\\fileb.txt']
'Basic Python' 카테고리의 다른 글
sort VS sorted (0) | 2023.11.10 |
---|---|
abspath, chdir, basename, os.path.join (0) | 2023.11.07 |
bytes VS bytearray (0) | 2023.10.30 |
pickle, dump, load (0) | 2023.10.24 |
sys.path, getcwd (0) | 2023.10.19 |