Skip to main content
added 624 characters in body
Source Link
Copperfield
  • 8.6k
  • 3
  • 26
  • 32

that is simple with sets, because it take care of the duplicates for you

Edit

with open('file1.txt',"a+") as file1, open('file2.txt') as file2:
    new_words = set(file2) - set(file1)
    if new_words:
        file1.write('\n') #just in case, we don't want to mix to words together 
        for w in new_words:
            file1.write(w)

Edit 2

If the order is important go with Max Chretien answer.

If you want to know the common words, you can use intersection

with open('file1.txt',"a+") as file1, open('file2.txt') as file2:
    words1 = set(file1)
    words2 = set(file2)
    new_words = words2 - words1
    common = words1.intersection(words2)
    if new_words:
        file1.write('\n')
        for w in new_words:
            file1.write(w)
    if common:
        print 'the commons words are'
        print common
    else:
        print 'there are no common words'

that is simple with sets, because it take care of the duplicates for you

Edit

with open('file1.txt',"a+") as file1, open('file2.txt') as file2:
    new_words = set(file2) - set(file1)
    if new_words:
        file1.write('\n')
        for w in new_words:
            file1.write(w)

Edit 2

If the order is important go with Max Chretien answer.

If you want to know the common words, you can use intersection

with open('file1.txt',"a+") as file1, open('file2.txt') as file2:
    words1 = set(file1)
    words2 = set(file2)
    new_words = words2 - words1
    common = words1.intersection(words2)
    if new_words:
        file1.write('\n')
        for w in new_words:
            file1.write(w)
    if common:
        print 'the commons words are'
        print common
    else:
        print 'there are no common words'

that is simple with sets, because it take care of the duplicates for you

Edit

with open('file1.txt',"a+") as file1, open('file2.txt') as file2:
    new_words = set(file2) - set(file1)
    if new_words:
        file1.write('\n') #just in case, we don't want to mix to words together 
        for w in new_words:
            file1.write(w)

Edit 2

If the order is important go with Max Chretien answer.

If you want to know the common words, you can use intersection

with open('file1.txt',"a+") as file1, open('file2.txt') as file2:
    words1 = set(file1)
    words2 = set(file2)
    new_words = words2 - words1
    common = words1.intersection(words2)
    if new_words:
        file1.write('\n')
        for w in new_words:
            file1.write(w)
    if common:
        print 'the commons words are'
        print common
    else:
        print 'there are no common words'
added 624 characters in body
Source Link
Copperfield
  • 8.6k
  • 3
  • 26
  • 32

that is simple with setssets, because it take care of the duplicates for you

Edit

with open('file1.txt',"a+") as file1, open('file2.txt') as file2:
    new_words = set(file2) - set(file1)
    if new_words:
        file1.write('\n')
        for w in new_words:
            file1.write(w)

Edit 2

If the order is important go with Max Chretien answer.

If you want to know the common words, you can use intersection

with open('file1.txt',"a+") as file1, open('file2.txt') as file2:
    words1 = set(file1)
    words2 = set(file2)
    new_words = words2 - words1
    common = words1.intersection(words2)
    if new_words:
        file1.write('\n')
        for w in new_words:
            file1.write(w)
    if common:
        print 'the commons words are'
        print common
    else:
        print 'there are no common words'

that is simple with sets, because it take care of the duplicates for you

Edit

with open('file1.txt',"a+") as file1, open('file2.txt') as file2:
    new_words = set(file2) - set(file1)
    if new_words:
        file1.write('\n')
        for w in new_words:
            file1.write(w)

that is simple with sets, because it take care of the duplicates for you

Edit

with open('file1.txt',"a+") as file1, open('file2.txt') as file2:
    new_words = set(file2) - set(file1)
    if new_words:
        file1.write('\n')
        for w in new_words:
            file1.write(w)

Edit 2

If the order is important go with Max Chretien answer.

If you want to know the common words, you can use intersection

with open('file1.txt',"a+") as file1, open('file2.txt') as file2:
    words1 = set(file1)
    words2 = set(file2)
    new_words = words2 - words1
    common = words1.intersection(words2)
    if new_words:
        file1.write('\n')
        for w in new_words:
            file1.write(w)
    if common:
        print 'the commons words are'
        print common
    else:
        print 'there are no common words'
added 32 characters in body
Source Link
Copperfield
  • 8.6k
  • 3
  • 26
  • 32

that is simple with sets, because it take care of the duplicates for you

Edit

with open('file1.txt',"a+") as file1, open('file2.txt') as file2, open('file3.txt','w') as file3:
    wordsnew_words = set(file2) - set(file1)
    wordsif new_words:
        file1.updatewrite(file2'\n')
        for w in wordsnew_words:
        file3    file1.write(w)

that is simple with sets, because it take care of the duplicates for you

with open('file1.txt') as file1, open('file2.txt') as file2, open('file3.txt','w') as file3:
    words = set(file1)
    words.update(file2)
    for w in words:
        file3.write(w)

that is simple with sets, because it take care of the duplicates for you

Edit

with open('file1.txt',"a+") as file1, open('file2.txt') as file2:
    new_words = set(file2) - set(file1)
    if new_words:
        file1.write('\n')
        for w in new_words:
            file1.write(w)
Source Link
Copperfield
  • 8.6k
  • 3
  • 26
  • 32
Loading