Search for a file using a wildcard Search for a file using a wildcard python python

Search for a file using a wildcard


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


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 sysprint 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


from glob import globimport sysfiles = glob(sys.argv[1])