Search for a file using a wildcard

I want get a list of filenames with a search pattern with a wildcard. Like:

getFilenames.py c:PathToFolder*
getFilenames.py c:PathToFolderFileType*.txt
getFilenames.py c:PathToFolderFileTypeA.txt

How can I do this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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

Method 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

Method 3

from glob import glob
import sys

files = glob(sys.argv[1])

Method 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)

Method 5

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:], []))


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x