How can I create search terms with wildcards in Python?
Use regular expressions and just loop through the file:
import re
f=open('test.file.here', 'r')
pattern = re.compile("^[^\s]*ello[^\s]*\sWorld[^\s]*$")
for line in f:
if pattern.match(line):
print line,
f.close()
I would usually opt for a regular expression, but if for some reason you want to stick to the wildcard format, you can do this:
from fnmatch import fnmatch
pattern = '*ello* World*'
with open('sample.txt') as file:
for line in f:
if fnmatch(line, pattern):
print(line)
The * syntax you describe is known as globbing. It doesn't work for documents, just files and directories. Regular expressions, as others have noted, are the answer.