[Solved] match string and delete lines up to matched string in python


Just check if each line contains the text you are looking for.

# Assuming the input file is called "input.txt"
with open('input.txt', 'r') as fin:
  # Read all the lines
  buff = iter(fin.readlines())

# For the output file do the following
with open('output.txt', 'w') as fout:
  # Iterate over every line
  for line in buff:
    # Check if the text you look for is not in the line
    if "train bus flight" not in line:
      # If not found check next line
      continue
    else:
      # Another for loop to start from where you are
      for line in buff:
        # Write the rest of the lines
        fout.write(line)

0

solved match string and delete lines up to matched string in python