I have two text files containing multiple lines of text, I need to compare both files and write the missing lines in the file two.
i will use a while loop for check every 60 seconds, i an idea but just work 1 pass

File one:

2012-10-04 01:03:11,50EF87C3.req,192.168.1.1,12312312312,OK -- HUAWEI Mobile Connect - 3G Modem #6,Text="1"
2012-10-04 01:18:59,50EF8BAD.req,192.168.1.1,12312312312,OK -- HUAWEI Mobile Connect - 3G Modem #6,Text="2"
2012-10-04 01:20:46,50EF8BAE.req,192.168.1.1,12312312312,OK -- HUAWEI Mobile Connect - 3G Modem #6,Text="3"
2012-10-04 01:21:33,50EF8BAF.req,192.168.1.1,12312312312,OK -- HUAWEI Mobile Connect - 3G Modem #6,Text="4"
2012-10-04 01:39:06,50EF8BB0.req,192.168.1.1,12312312312,OK -- HUAWEI Mobile Connect - 3G Modem #6,Text="5"

File two:

2012-10-04 01:03:11,50EF87C3.req,192.168.1.1,12312312312,OK -- HUAWEI Mobile Connect - 3G Modem #6,Text="1"
2012-10-04 01:18:59,50EF8BAD.req,192.168.1.1,12312312312,OK -- HUAWEI Mobile Connect - 3G Modem #6,Text="2"
2012-10-04 01:20:46,50EF8BAE.req,192.168.1.1,12312312312,OK -- HUAWEI Mobile Connect - 3G Modem #6,Text="3"

Recommended Answers

All 3 Replies

You can make set of lines in file two and one and difference is the lines to add to file two. If order does not matter you can add those line at end of file two by reopening in append mode the file two, otherwise add to lines already read in desired place (for example sort) and rewrite the whole file.

i just solve my problem:

def grabartxt(texto):
    archi=open(archivo,'a')
    archi.write(texto[2:])    
    archi.close()

# find the difference between two texts
# tested with Python24   vegaseat  6/2/2005

text2 =  (file(archivo).read())
text1=  (file(originales).read())

# create a list of lines in text1
text1Lines = text1.splitlines(1)
print "Lines of text1:"
for line in text1Lines:
  print line,
print
# dito for text2
text2Lines = text2.splitlines(1)
print "Lines of text2:"
for line in text2Lines:
  print line,
print  
diffInstance = difflib.Differ()
diffList = list(diffInstance.compare(text1Lines, text2Lines))
print '-'*50
print "Lines different in text1 from text2:"
for line in diffList:
  if line[0] == '-':
    print line,
    grabartxt(line)

You could simplify this a little with ...

lines1 = open("Text1.txt").readlines()
lines2 = open("Text2.txt").readlines()

# set up module difflib
diff_instance = difflib.Differ()
diff_list = list(diff_instance.compare(lines1, lines2))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.