Python 3.x outputting a text file with names of files that contain a list of words
<p>I have approximately 160,000 text files in a directory. My first objective is to create a list of files that contain at least one item from a list of about 50 keywords. My current code is </p>

<pre><code>import os
ngwrds= [list of words]

for filename in os.listdir(os.getcwd()):

        with open(filename, 'r') as searchfile:
            for line in searchfile:
                if any(x in line for x in ngwrds):
                    with open("keyword.txt", 'a') as out:
                        out.write(filename + '\n')
</code></pre>

<p>Which works but sends out duplicate filenames. Ideally what I would like is for the loop to stop once it hits the first keyword, write the file name to 'keyword.txt', and move on to the next file in the directory. Any thoughts on how to do this?</p>