glob module
glob is used to find files that have specific pattern.
glob is one of the most simple module and there are only three wildcard “*”, “?”, “[]”.
- ”*” match 0 or more characters
- ”?” match a single character
- ”[ ]” match character within a range, like [0-9] to match digit.
Suppose we have a directly with the following files.
dir
dir/file.txt
dir/file1.txt
dir/file2.txt
dir/filea.txt
dir/fileb.txt
dir/subdir
dir/subdir/subfile.txt
Match all files.
Use * to match any characters. glob.glob is very common, which return a list. Also you can use glob.iglob, which return a generator.
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
Macth files in subdirectory
Subdirectory can be specified or use wildcard instead.
print 'Named explicitly:'
for name in glob.glob('dir/subdir/*'):
print '\t', name
print 'Named with wildcard:'
for name in glob.glob('dir/*/*'):
print '\t', name
Named explicitly:
dir/subdir/subfile.txt
Named with wildcard:
dir/subdir/subfile.txt
Single character matching
Apart from *, ? also can be used. For example, if we want to match files that start with file, end with .txt, between is any character.
for name in glob.glob('dir/file?.txt'):
print name
dir/file1.txt
dir/file2.txt
dir/filea.txt
dir/fileb.txt
Matching digit[0-9]
Match a file with digit before file extension.
for name in glob.glob('dir/*[0-9].*'):
print name
dir/file1.txt
dir/file2.txt
