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.

Possibly Related Threads…
Thread Author Replies Views Last Post
  code to read files in folders and transfer the file name, type, date created to excel Divya577 0 120 Dec-06-2020, 04:14 PM
Last Post: Divya577
  Can't open files Lass86 5 275 Nov-10-2020, 07:18 PM
Last Post: jefsummers
  Split gps files based on time (text splitting) dervast 0 133 Nov-09-2020, 09:19 AM
Last Post: dervast
  Searching for specific word in text files. JellyCreeper6 1 204 Nov-03-2020, 01:52 PM
Last Post: DeaD_EyE
  How to read csv files parallay Mekala 2 223 Oct-24-2020, 07:33 AM
Last Post: Mekala
  Counting the most relevant words in a text file caiomartins 2 298 Sep-21-2020, 08:39 AM
Last Post: caiomartins
  Using Python to loop csv files to open them Secret 4 487 Sep-13-2020, 11:30 AM
Last Post: Askic
  How to work with multiple files and tkinter? philipbergwerf 3 458 Aug-31-2020, 12:03 PM
Last Post: ndc85430
  read terminal text from running program AArdvark_UK 2 316 Aug-27-2020, 12:43 PM
Last Post: AArdvark_UK
  Find specific subdir, open files and find specific lines that are missing from a file tester_V 8 761 Aug-25-2020, 01:52 AM
Last Post: tester_V

Forum Jump:

User Panel Messages

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