no need to read the whole file in memory, just iterate over lines
in_file = 'file1.txt' out_file = 'output.txt' with open(in_file, "r") as f1, open(out_file, "w+") as f2: for line in f1: if line.startswith('comb'): pass # skip lines that start with 'comb' else: if line.startswith('result'): line = line[6:].lstrip() # remove result and strip whitespace from left-hand side f2.write(line)now, with file2 there will be double blank lines (i.e. that is the blank lines before and after comb part). you will need to fix this if it is a problem. Also note that your blank lines in the example have single space (not actually empty lines).
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs