Search for a file using a wildcard

81,939

Solution 1

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

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

from glob import glob
import sys

files = glob(sys.argv[1])

Solution 4

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)
Share:
81,939
Stan
Author by

Stan

Updated on July 09, 2022

Comments

  • Stan
    Stan almost 2 years

    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?