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 8121397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:28:40+00:00 2026-06-06T05:28:40+00:00

I have two text file. file1.txt has: gedit google chrome git vim foo bar

  • 0

I have two text file.

file1.txt has:

gedit
google chrome
git
vim
foo
bar

file2.txt has:

firefox
svn
foo
vim

How do I write a script that when executed (with file1.txt and file2.txt as argument) checks for duplication of text in each line (I mean it should process line wise), deletes the duplicated text in both the files.

So after the processing, both file1.txt and file2.txt should have following contents:

gedit
google chrome
git
bar
firefox
svn

Notice that foo and vim has been removed from both files.

Any guidance?

  • 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-06-06T05:28:42+00:00Added an answer on June 6, 2026 at 5:28 am
    with open('file1.txt','r+') as f1 ,open('file2.txt','r+') as f2:
        file1=set(x.strip() for x in f1 if x.strip())
        file2=set(x.strip() for x in f2 if x.strip())
        newfile=file1.symmetric_difference(file2) #symmetric difference removes those values which are present in both sets, and returns a new set.
        f2.truncate(0) #truncate the file to 0 bytes
        f1.truncate(0)
        f2.seek(0) # to push the cursor back to the starting pointing in the file.
        f1.seek(0)
        for x in newfile:
            f1.write(x+'\n')
            f2.write(x+'\n')
    

    now both files contains:

    svn
    git
    firefox
    gedit
    google chrome
    bar
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two 3GB text files, each file has around 80 million lines. And
I'm using python 2.6 on linux. I have two text files first.txt has a
I have a text file containing two or more types of lines. I would
I have to filter a text file filter.tmp containing two types of lines, this
i have two text files file 1 number,name,account id,vv,sfee,dac acc,TDID 7000,john,2,0,0,1,6 7001,elen,2,0,0,1,7 7002,sami,2,0,0,1,6 7003,mike,1,0,0,2,1
I have zip file inside a Text file (file.txt inside a file.zip) and I
I have two columns in a text file. I read them into Python into
I have a text file with numbers: two numbers on each row, separated by
I have a text file that has following content: ac und accipio annehmen ad
I have two text files, Source.txt and Target.txt. The source will never be modified

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.