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?
Simply, the wildcard ? lets you replace it for any character in a search. This means that if you're looking for a file and you're not sure how it is spelled, you can simply substitute “?” for the characters you don't know. In the following example, we search for files that start with “img_2” and end with “. jpg”.
Wildcard character is a special character that represents one or more other characters. Wildcard characters are often used when you do not know the exact name of file/folder or you do not want to type the entire name. There are two wildcard characters: asterisk (*) and question mark (?).
Wildcard SearchesTo perform a multiple character wildcard search use the "*" symbol. You can also use the wildcard searches in the middle of a term. Note: You cannot use a * or ? symbol as the first character of a search.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With