2

I have a Two text files ,names are one.txt and two.txt In one.txt , contents are

AAA
BBB
CCC
DDD

In two.txt , contents are

DDD
EEE

I want a python code to determine a contains of two.txt are present in one.txt or not If present means, don't do anything, But if contents of two.txt are not present means,it should append to one.txt

I want a output in one.txt as

AAA
BBB
CCC
DDD
EEE

Code:

file1 = open("file1.txt", "r") 
file2 = open("file2.txt", "r") 
file3 = open("resultss.txt", "w") 
list1 = file1.readlines() 
list2 = file2.readlines() 
file3.write("here: \n") 
for i in list1: for j in list2: 
   if i==j: file3.write(i)
4
  • Please edit your code directly into the question. Commented May 9, 2017 at 13:45
  • Please add the code to your question and indent it by four spaces to preserve formatting; comments are not suitable for this, especially for a whitespace-dependent language like Python :) Commented May 9, 2017 at 13:45
  • Does the resultant file need to have any particular order? In your example, your two source files are sorted--is that to be assumed? May all three files be in memory at the same time (so the files are not very large)? Commented May 9, 2017 at 13:53
  • No need to in order format @ Rory Daulton Commented May 9, 2017 at 13:54

3 Answers 3

4

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'
Sign up to request clarification or add additional context in comments.

6 Comments

How about set(file1) | set(file2)?
Actually i want result in file1.txt itself , no need of output file @Copperfield
@stamaimer that work too, but I think that update don't create an intermediary throw away set
@Copperfield, new words value will provide a difference between the files,right? Just Curious-Is there any way to get the already presented words present in file2.txt..
@pavithranG yes, with intersection you get the words present in both files
|
1

This should do it:

with open('file1.txt', 'r+') as file1, open('file2.txt') as file2:
    f1 = [i.strip() for i in file1.readlines()]
    f2 = [j.strip() for j in file2.readlines()]
    f1 += [item for item in f2 if item not in f1]
    file1.seek(0)
    for line in f1:
        file1.write(line + '\n')

Comments

1

Similar solution using set maybe a bit shorter...

with open('one.txt', 'r+') as f_one, open('two.txt', 'r') as f_two:
    res = sorted(set(f_one) | set(f_two))
    f_one.seek(0)
    f_one.writelines(res)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.