How to capture the next string after matching in Python

advertisements

Suppose my text file contains a string key followed by different strings. How I can capture the next string and write it inside a file in Python.

if 'key' in open('Internet.txt').read():
    f=open("pathex","w")
    f.write("written")


You can read file line by line and if key was there write a written into destination file:

 f = open("pathex.txt" , "w")
 with open('Internet.txt', 'r') as file_source:
    for line in file_source:
        if key in line:
            f.write("written")