Search for a file using a wildcard
PythonFileWildcardPython Problem Overview
I want get a list of filenames with a search pattern with a wildcard. Like:
getFilenames.py c:\PathToFolder\*
getFilenames.py c:\PathToFolder\FileType*.txt
getFilenames.py c:\PathToFolder\FileTypeA.txt
How can I do this?
Python Solutions
Solution 1 - Python
You can do it like this:
>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']
Note:
If the directory contains files starting with .
they won’t be matched by default. For example, consider a directory containing card.gif
and .card.gif
:
>>> import glob
>>> glob.glob('*.gif')
['card.gif']
>>> glob.glob('.c*')
['.card.gif']
This comes straight from here: http://docs.python.org/library/glob.html
Solution 2 - Python
glob
is useful if you are doing this in within python, however, your shell may not be passing in the *
(I'm not familiar with the windows shell).
For example, when I do the following:
import sys
print sys.argv
On my shell, I type:
$ python test.py *.jpg
I get this:
['test.py', 'test.jpg', 'wasp.jpg']
Notice that argv
does not contain "*.jpg"
The important lesson here is that most shells will expand the asterisk at the shell, before it is passed to your application.
In this case, to get the list of files, I would just do sys.argv[1:]
. Alternatively, you could escape the *
, so that python sees the literal *
. Then, you can use the glob
module.
$ getFileNames.py "*.jpg"
or
$ getFileNames.py \*.jpg
Solution 3 - Python
from glob import glob
import sys
files = glob(sys.argv[1])
Solution 4 - Python
If you're on Python 3.5+, you can use pathlib
's glob()
instead of the glob
module alone.
Getting all files in a directory looks like this:
from pathlib import Path
for path in Path("/path/to/directory").glob("*"):
print(path)
Or, to just get a list of all .txt
files in a directory, you could do this:
from pathlib import Path
for path in Path("/path/to/directory").glob("*.txt"):
print(path)
Finally, you can search recursively (i.e., to find all .txt
files in your target directory and all subdirectories) using a wildcard directory:
from pathlib import Path
for path in Path("/path/to/directory").glob("**/*.txt"):
print(path)
Solution 5 - Python
I am adding this to the previous because I found this very useful when you want your scripts to work on multiple shell and with multiple parameters using *
.
If you want something that works on every shells, you can do the following (still using glob
):
>>> import glob
>>> from functools import reduce # if using python 3+
>>> reduce(lambda r, x: r + glob.glob(x), sys.argv[1:], [])
Note that it can produce duplicate (if you have a test
file and you give t*
and te*
), but you can simply remove them using a set
:
>>> set(reduce(lambda r, x: r + glob.glob(x), sys.argv[1:], []))