Python Forum
Open and read multiple text files and match words
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Open and read multiple text files and match words

How can I create a script that reads two text files and prints out words that match within text file number 1?
This code below is the furthest I got, it can match words in string and print it out, but I need it to read two or more large text files and print same found matched words. Thank you.

import re

def get_words_from_string(s):
    return set(re.findall(re.compile('\w+'), s.lower()))

def get_words_from_file(fname):
    with open(fname, 'rb') as inf:
        return get_words_from_string(

def all_words(needle, haystack):
    return set(needle).issubset(set(haystack))

def any_words(needle, haystack):
    return set(needle).intersection(set(haystack))

search_words = get_words_from_string("this my test")
find_in = get_words_from_string("If this were my test, I is passing")

print (search_words)
This can be compacted by using list comprehension, but will do the job
import os

def get_words(filename):
    wordlist = []
    with open(filename) as fp:
        for line in fp:
            wordsinline = line.strip().split()
            for item in wordsinline:
                if item not in wordlist:
    return wordlist

def find_common_words(filename1, filename2):
    wordlist1 = []
    wordlist2 = []
    matching_words = []

    wordlist1 = get_words(filename1)
    wordlist2 = get_words(filename2)

    matching_words = set(wordlist1) & set(wordlist2)

def testit():
    # Assert in same directory as code
    filename1 = 'words1.txt'
    filename2 = 'words2.txt'
    find_common_words(filename1, filename2)

if __name__ == '__main__':
Thank you my man, love you.
I see my varbatim response on stackoverflow how nice.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Extract a string between 2 words from a text file OscarBoots 2 275 Nov-02-2021, 08:50 AM
Last Post: ibreeden
  Process multiple pdf files Spartan314 1 225 Oct-27-2021, 10:46 PM
Last Post: Larz60+
  [SOLVED] Read text file from some point till EOF? Winfried 1 282 Oct-10-2021, 10:29 PM
Last Post: Winfried
  Generate Multiple sql Files With csv inputs vkomarag 13 1,184 Aug-20-2021, 07:03 PM
Last Post: vkomarag
  Sorting and Merging text-files AlphaInc 10 1,061 Aug-20-2021, 05:42 PM
Last Post: snippsat
  How to open/load image .tiff files > 2 GB ? hobbyist 1 477 Aug-19-2021, 12:50 AM
Last Post: Larz60+
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 554 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Replace String in multiple text-files [SOLVED] AlphaInc 5 1,039 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
  Several pdf files to text mfernandes 10 1,515 Jul-07-2021, 11:39 PM
Last Post: Pedroski55
  Plotting Multiple files ! Helen_145 1 639 Jun-26-2021, 03:28 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020