0

I have the following python code which is supposed to find the word "ACT" in a file. Once it finds "ACT" it should write all the lines that follow onto the output file. My code writes everything from the input file into the output file. What am I doing wrong?

    found_ACT = False
    for line in inputFile:
        line.strip()
        if found_ACT:
            outputFile.write(line)
        else:
            if "ACT" in line:
                found_ACT = True
Aron Tesfay
  • 313
  • 1
  • 2
  • 11
  • 1
    This is not a [MCVE] (for one, the indentation is all wrong, and for another, I'm pretty sure it would work correctly). Make sure it is actually verifiable (someone can run it with a given input and reproduce your incorrect output). – ShadowRanger Mar 28 '18 at 02:39
  • This recent answer may help: https://stackoverflow.com/questions/49525317/how-to-strip-the-beginning-of-a-file-with-python-library-re-sub – Vivek Pabani Mar 28 '18 at 03:19
  • Can you post your text? – Rakesh Mar 28 '18 at 05:33
  • Thanks. It actually works but there was another "ACT" string hiding in the file that preceded the one I was targetting – Aron Tesfay Mar 28 '18 at 11:38

1 Answers1

0

You never change found_ACT to False if it is not found in line. You should put found_ACT = False inside the loop, or just use "ACT" in line directly.

for line in inputFile:
    line.strip()
    if "ACT" in line:
        outputFile.write(line)
Lily Yung
  • 60
  • 9