Search code examples
pythonfilefile-comparison

Comparing some data of a file with another file in python


I do have a file f1 which Contains some text lets say "All is well". In Another file f2 I have maybe 100 lines and one of them is "All is well".

Now I want to see if file f2 contains content of file f1.

I will appreciate if someone comes with a solution.

Thanks


Solution

  • with open("f1") as f1,open("f2") as f2:
        if f1.read().strip() in f2.read():
             print 'found'
    

    Edit: As python 2.6 doesn't support multiple context managers on single line:

    with open("f1") as f1:
        with open("f2") as f2:
           if f1.read().strip() in f2.read():
                 print 'found'