Comparing two directories with same name text files and unmatch regex findall

Viewed 351

I have two directories in which I have text files with same matching file names. I need to compare same name texts files respectively and write out the unmatched words in csv with respective file name.

Folder1:
  file1.txt   file2.txt

Folder2: 
  file1.txt   file2.txt

Here i need findall unmatched words from comparing the two. I have something like below

stat="files, unmatch_words\n"
pack=os.list('./Folder1/')
for file in pack:
    package=open('./Folder1/' + file, 'r').read()
    grep=open("./Folder2/" + file,'r').read()
    output= re.findall(r'(\w+)',grep)
    rex=(set(output))
    stat += file.replace('.txt', '') + ',"'

    for sem in rex:
        if sem not in package:
            stat += sem + '\n'
stat += '","'
stat += '"' + '\n'
f=open('file.csv', 'w')
f.write(stat)

This combines all files in Folder2 (i.e rex) in to one and comparing it with files in Folder1.

I want to unmatch words from Folder1 -> file1.txt with Folder2 -> file1.txt like wise Folder1 -> file2.txt with Folder2 -> file2.txt

Can anyone please suggest any update on this. Thanks

3 Answers

This kind of functionality is trivial with difflib

Example:

Given these two files:

% cat file1.txt
line 1
line 2
blah bligh blah line 3
line 4
line 5
% cat file2.txt
line 1
blip blop bloop line 2
line 3
line 4
line 5

The unix diff utility shows this difference:

% diff file1.txt file2.txt
2,3c2,3
< line 2
< blah bligh blah line 3
---
> blip blop bloop line 2
> line 3

You can trivially replicate diff with Python's difflib:

from difflib import Differ 
d=Differ()

with open(f1_name) as f1, open(f2_name) as f2:  
    print(''.join(d.compare(f1.readlines(),f2.readlines())))

Prints:

  line 1
+ blip blop bloop line 2
- line 2
?      ^
+ line 3
?      ^
- blah bligh blah line 3
  line 4
  line 5

To replicate this with a regex alone is not really possible.

The four character ' ', '+', '?', '-' allow you to programmatically separate which file has which change (left or right) you could then write those changes to a csv file.

If you poke around in difflib you will likely get the direction you need on this.

If you are only looking for word by word differences, you can use a set:

with open(f1_name) as f1, open(f2_name) as f2:      
    s1={word for line in f1 for word in line.split()}
    s2={word for line in f2 for word in line.split()}

>>> s1-s2
{'blah', 'bligh'}            # words only in file1
>>> s2-s1
{'blip', 'blop', 'bloop'}    # words only in file2

I wouldn't use regex personally. You can split the words by whitespaces, put them into two lists and check the unmatched:

    #get words in those files, splited by whitespaces
    packageWords=package.split()
    grepWords=grep.split()
    #words in package but not in grep
    unmatchedWordsPackage=[word for word in packageWords if not word in grepWords]
    #words in grep but not in package
    unmatchedWordsGrep=[word for word in grepWords if not word in packageWords]
    #merge the two lists into one big string
    unmatchedWords=' '.join(unmatchedWordsGrep) + ' ' + ' '.join(unmatchedWordsGrep)
import os, csv
path1 = "stackoverflow/Folder1"
path2 = "stackoverflow/Folder2"
files = os.listdir(path1)
for file in files: 
    folder1_data = open("%s/%s" % (path1, file), 'r').read() 
    folder2_data = open("%s/%s" % (path2, file), 'r').read() 
    folder1_data = folder1_data.replace("\n", " ") 
    folder2_data = folder2_data.replace("\n", " ") 
    folder1_data = folder1_data.strip() 
    folder1_data = folder1_data.split(" ") 
    folder2_data = folder2_data.strip() 
    folder2_data = folder2_data.split(" ") 
    output = list(set(folder1_data).union(set(folder2_data)) - set(folder1_data).intersection(set(folder2_data))) 
    with open("stackoverflow/" + file.replace("txt", "csv"), 'w') as csv_file: 
        file_writer = csv.writer(csv_file) 
        file_writer.writerow(output) 
Related