Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:\PathToFolder\FileType*.txt getFilenames.py c:\PathToFolder\FileTypeA.txt 

How can I do this?

like image 748
Stan Avatar asked Jul 27 '10 23:07

Stan


People also ask

How do I search for a file using a wildcard?

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”.

Which are the wildcard characters for finding a file folder?

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 (?).

Can I do a wildcard search on Google?

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.


1 Answers

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

like image 79
Martin Avatar answered Sep 26 '22 00:09

Martin