1

I am trying to make a function which will compare two txt files. If it recognizes new lines that are in one file but not in the other, it will add them in a list and also in that file that does not contain those new lines. It fails to do that. Here is my function. What am I doing wrong?

newLinks = []

def newer():
with open('cnbcNewLinks.txt', 'w') as newL:
    for line in open('cnbcCleanedLinks.txt'):
        if line not in "cnbcNewLinks.txt":
            newLinks.append(line)
            newL.write(line)
        else:
            continue
cleaned = ''.join(newLinks)
print(cleaned)
hcerim
  • 959
  • 1
  • 11
  • 27

2 Answers2

1

If files not big, then move data in list, both of list convert in set and use 'differ' builtin functions, two times. then add difference in files.

Alex
  • 37
  • 4
1

I put in python code what @Alex suggested.

See the doc for set.

I replace you text file name by a.txt and b.txt to be easily readable.

# First read the files and compare then using `set`
with open('a.txt', 'r') as newL, open('b.txt', 'r') as cleanL:
    a = set(newL)
    b = set(cleanL)
    add_to_cleanL = list(a - b) # list with line in newL that are not in cleanL
    add_to_newL = list(b - a) # list with line in cleanL that are not in newL

# Then open in append mode to add at the end of the file
with open('a.txt', 'a') as newL, open('b.txt', 'a') as cleanL:
    newL.write(''.join(add_to_newL)) # append the list at the end of newL
    cleanL.write(''.join(add_to_cleanL)) # append the list at the end of cleanL
Kruupös
  • 5,097
  • 3
  • 27
  • 43