Thursday, 5 May 2011

Python program for printing common lines in two files.

hi,

today i made a simple python program to print common lines in two files.

the code is given below...

f1 = open('words.txt').readlines()
f2 = open('words1.txt').readlines()


if len(file1lines) != 0 | len(file2lines) != 0:
    for line in set(f1):
        for lines in set(f2):
            if line == lines:
                print line.rstrip('\n')

f1.close
f2.close

snap -explanation:

reading multiple lines from two files -  using 'set' to  eliminate repeated lines - comparing two files with for loop - printing the common lines.

i/p:

(word.txt)

this is a text string
this is another string

(word1.txt)

hello therer
this is a text string
actually really
sundelly peruchally    
this is another string
this is a text string

o/p:

this is a text string
this is another string


No comments:

Post a Comment