সোমবার, ২৮ ফেব্রুয়ারী, ২০১১

Comparinr two text file in python.

Python has a very rich IO features.Couple of days ago I was just trying to read a simple text file.
Found interesting method like read() and readline().

When I coded  iterative loop for different files,python warns and says "iteration can loose data".
So,I just worked through the list,reading two files and keeping the lines as lits and then by comaring those list creating a new list.

And at last write in to a new file.
My code was as follows:
#!/usr/bin/python
f_r = open('/Users/coder/Desktop/play_py/read.txt', 'r')
#read_data = f_r.read()
f_w = open('/Users/coder/Desktop/play_py/write.txt', 'r')
list_two = []
list_one = []
list_three = []
for line in f_r:
    list_one.append(line)
for line in f_w:
    list_two.append(line)
#list_three = set(list_one) & set(list_two)
#a = str(list_three)
for item in list_one:
    if not item in list_two:
        list_three.append(item)
a = str(list_three)
new_file = open('/Users/coder/Desktop/play_py/new.txt','w')
new_file.write(a)
new_file.close()
f_r.close()
f_w.close()

It has the out put written in a list from.