Skip to content
Advertisement

Get the lines of txt file which not the first part or not exist in another file’s lines in python

I have two txt files and need to get the output in a new txt file :

one file has the below (named f.txt):

JavaScript

and another file has the below lines (named x.txt):

JavaScript

i need to get the lines of f.txt file which not the first part or not exist in x.txt lines to get the output like below :

JavaScript

and here is my python code but its not working for me , so thanks to help please .

JavaScript

Advertisement

Answer

  1. read all line from both files

  2. remove the last 'n' of each line

  3. check whether the line is valid, use a valid flag here :

  • initialize valide flag as ‘True’

  • if any line is start with the checking line, mark the valid flag as ‘False’

  • filter lines by the valid flag

  1. write the result to the result file
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement