How to Find Unique Lines from two Text(.txt) Files in Python?

In this article, let us see how to use Python to find only the unique lines in two text files (.txt files). We may also state that we will use Python to remove the lines that already exist in both of the text (.txt) files and place the new lines in another.txt file.

Let us consider the below two text files as an example:

demotextfile_1.txt:

hello this is Python-programs
good morning all

demotextfile_2.txt:

hello this is Python-programs
Are you good at coding? 
Then give a start to it
welcome to the greatest python coding platform
good morning all

The output of our code must be as follows:

Are you good at coding? 
Then give a start to it
welcome to the greatest python coding platform

Finding Unique Lines from two Text(.txt) Files in Python

Approach:

  • Open some random text file in read-only mode using the open() function and read all the lines of the file using the readlines() function.
  • Store it in a variable.
  • Similarly, Open the second text file in read-only mode using the open() function and read all the lines of the file using the readlines() function.
  • Store it in another variable.
  • Take a new empty list and store it in another variable.
  • Loop in the given second text file using the for loop
  • Check if each line of the second file is NOT present in the given first text file using the if conditional statement.
  • If it is true(unique), then append that line to the above created new list
  • Open some random new file in write mode using the open() function
  • Loop the above created new list(which contains unique lines) using the for loop
  • Write the corresponding line to the given output file using the write() function
  • The Exit of the Program.

Below is the implementation:

# Open some random text file in read-only mode using the open() function 
# and read all the lines of the file using the readlines() function.
# Store it in a variable.
gvn_txtfile_1= open('demotextfile_1.txt','r').readlines()
# Similarly, Open the second text file in read-only mode using the open() function 
# and read all the lines of the file using the readlines() function.
# Store it in another variable.
gvn_txtfile_2 = open('demotextfile_2.txt','r').readlines()
# Take a new empty list and store it in another variable.
lines_lst= []
# Loop in the given second text file using the for loop
for line in gvn_txtfile_2:
  # Check if each line of the second file is NOT present in the given first text 
  # file using the if conditional statement.
  if line not in gvn_txtfile_1:
    # If it is true(unique), then append that line to the above created new list
    lines_lst.append(line)

# Open some random new file in write mode using the open() function 
with open('output_file.txt','w') as outfile:
    # Loop the above created new list(which contains unique lines) using the for loop
    for line in lines_lst:
        # Write the corresponding line to the given outputfile using the write() function
        outfile.write(line)

Output:(output_file.txt)

Are you good at coding? 
Then give a start to it
welcome to the greatest python coding platform

NOTE: If the file output_file.txt already exists, it will simply be updated.