The specific exception you asked about is because line
doesn't exist outside the generator expression. If you want to access it, you need to keep it in the same scope as the print
statement, like this:
for line in f:
needle = line.strip()
if needle in haystack:
print line
But this isn't going to be particularly useful. It's just going to be the word from needle
plus the newline at the end. If you want to print out the line (or lines?) from haystack
that include needle
, you have to search for that line, not just ask whether needle
appears anywhere in the whole haystack
.
To literally do what you're asking for, you're going to need to loop over the lines of haystack
and check each one for needle
. Like this:
with open('something.txt') as f:
haystacks = list(f)
with open('d:\\Users\\something\\Desktop\\something\\dictionary\\entirelist.txt') as f:
for line in f:
needle = line.strip()
for haystack in haystacks:
if needle in haystack:
print haystack
However, there's a neat trick you may want to consider: If you can write a regular expression that matches any complete line that includes needle
, then you just need to print out all the matches. Like this:
with open('something.txt') as f:
haystack = f.read()
with open('d:\\Users\\something\\Desktop\\something\\dictionary\\entirelist.txt') as f:
for line in f:
needle = line.strip()
pattern = '^.*{}.*$'.format(re.escape(needle))
for match in re.finditer(pattern, haystack, re.MULTILINE):
print match.group(0)
Here's an example of how the regular expression works:
^.*Falco.*$

Debuggex Demo
Of course if you want to search case-insensitively, or only search for complete words, etc., you'll need to make some minor changes; see the Regular Expression HOWTO, or a third-party tutorial, for more.