I am looking for some file names in a list using a wildcard.
from datetime import date
dt = str("RT" + date.today().strftime('%d%m'))
print dt # RT0701
Basically I need to find this pattern dt + "*.txt"
:
RT0701*.txt
In this list:
l = ['RT07010534.txt', 'RT07010533.txt', 'RT02010534.txt']
How can i do that with a for loop?
Wildcards (*,?,~) can be used in conditions to represent one or more characters. The & character is used to concatenate, or join, two or more strings or the contents of referenced cells. Some examples of the use of the concatenation operator are: "Abc"&"Def" returns "AbcDef".
The asterisk ( ∗) An asterisk ∗ is used to specify any number of characters. It is typically used at the end of a root word. This is great when you want to search for variable endings of a root word. For example, searching for work* would tell the database to look for all possible word-endings to the root “work”.
Wildcard SearchesTo perform a single character wildcard search use the "?" symbol. To perform a multiple character wildcard search use the "*" symbol. You can also use the wildcard searches in the middle of a term.
To use wildcards, you will need to use the Find and Replace dialog box and expand it to display more options. You can then select the option to Use wildcards. A wildcard can replace one or more characters in a string of text or numbers.
You can use fnmatch.filter()
for this:
import fnmatch
l = ['RT07010534.txt', 'RT07010533.txt', 'RT02010534.txt']
pattern = 'RT0701*.txt'
matching = fnmatch.filter(l, pattern)
print(matching)
Outputs:
['RT07010534.txt', 'RT07010533.txt']
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