Replace line containing string with Python : <p>I'm trying to search through a file for a line that contain certain text and then replace that entire line with a new line.</p>

<p>I'm trying to use:</p>

<pre><code>pattern = "Hello"
file = open('C:/rtemp/output.txt','w')

for line in file:
    if pattern in line:
        line = "Hi\n"
        file.write(line)
</code></pre>

<p>I get an error saying:</p>

<pre><code>io.UnsupportedOperation: not readable
</code></pre>

<p>I'm not sure what I'm doing wrong, please can someone assist.</p>
