Search Specific Keyword From a File and add description associated to it to different file with Python
<p>Below text file shows the Description of Channel Name &amp; the Channel Event</p>

<p><strong><em>desc.txt file:</em></strong></p>

<pre><code>Channel Name: CBS
Event Name: FIFA World Cup 2018 Qualifying

Channel Name: BEINSPORTS
Event Name: NFL 
</code></pre>

<p>Below Python code only looks for the First "Event Name" and associated description. </p>

<pre><code>flist = open('/tmp/desc.txt').readlines()
    for line in flist:
        if line.startswith("Event Name:"):
            eventname = line[12:-1]                  
            file2 = open('/tmp/test_live.txt','w')
            file2.write(eventname + "\n")
</code></pre>

<p>What I want is the below description and what I am getting currently is 
"FIFA World Cup 2018 Qualifying" in the file. </p>

<p><strong><em>test_live.txt</em></strong></p>

<pre><code>FIFA World Cup 2018 Qualifying
NFL
</code></pre>

<p>You keep opening the same file and writing one line to it. You have two options: You can either open it once outside the loop (more efficient), and close it when exiting the loop (you should close your other <code>open</code> statement too). Or you can open it with <code>'a'</code> instead of <code>'w'</code> to <em>append</em> to the file.
But I would just open it before going into the <code>for</code> loop. 
Also your loop should not be indented under the <code>flist</code> line unless you use <code>with</code>... </p>

<p>Note: I think typically you should use <code>.rstrip()</code> instead of truncating the <code>\n</code> with <code>[:-1]</code> The last line might not have a newline at the end, and you would lose that character.</p>

<pre><code>with open('/tmp/desc.txt') as flist:
    file2 = open('/tmp/test_live.txt','w')
    for line in flist:
        if line.startswith("Event Name:"):
            eventname = line[12:-1]                  
            file2.write(eventname + "\n")
    file2.close()
</code></pre>

python file python-3.x