Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6893405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:39:16+00:00 2026-05-27T06:39:16+00:00

I have a problem when I try to compare two large files. What I

  • 0

I have a problem when I try to compare two large files. What I am trying to do is take a line from one file, search all the lines of another file for a match and if there isn’t one, write that line to another file. I was able to recreate the problem with the simple example below:

file1.txt (contents)

apple
banana
pear
peach
lime

file_old.txt (contents)

lime
apple
pear
peach

Since I am looking for lines in file1 that are not in file_old, I would expect that ‘banana’ would be the only value to show in the output file. But in the output file, “*fill_diff*”, I am showing:

apple
banana
banana

What is wrong with my code to try and produce the differences in a file?

def main():

    file_old = open(r'C:\Users\test\Desktop\file_old.txt', 'r+')
    file_new = open(r'C:\Users\test\Desktop\file1.txt', 'r+')
    file_diff = open(r'C:\Users\test\Desktop\file_diff.txt', 'w')

    for each_line in file_new: 
        for every_line in file_old:
            if each_line == every_line:
                break
            file_diff.write(each_line)

    file_old.close()
    file_new.close()
    file_diff.close()

main()

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:39:16+00:00Added an answer on May 27, 2026 at 6:39 am

    srgerg’s answer will work.

    However, reading through files multiple times will have a very large runtime complexity. Therefore, if the files (though large) are small enough to fit into memory, then you might consider putting all the lines in file_old into a data structure for comparison:

    old_lines = set((line.strip() for line in open(r'C:\Users\test\Desktop\file_old.txt', 'r+')))
    file_new = open(r'C:\Users\test\Desktop\file1.txt', 'r+')
    file_diff = open(r'C:\Users\test\Desktop\file_diff.txt', 'w')
    
    for line in file_new:
        if line.strip() not in old_lines:
            file_diff.write(line)
    file_new.close()
    file_diff.close()
    

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two files I've been trying to compare with diff. The files are
I have a problem using the SSIS. I try to import data from database
i'm download DrillDownApp example project from iPhoneSDKArticles i have a problem when i'm try
I am trying to test the API from ankoder.com and have problem on the
I am having a problem when i am trying to compare two dates in
I have a serious problem. Firstly, I am trying to retrieve values from my
I have problem when I try insert some data to Informix TEXT column via
I have a problem when I try to persist my model. An exception is
I have this problem when i try to synchronize a observable list with listbox/view
I have problem with base classes in WPF. I try to make a base

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.