How to dump results of for loop into a .txt file using Python? : <p>I am working with this python script. I want it to dump all hosts which are alive into a text file and discard all hosts which don't respond to the ping. Currently I have this part.</p>

<pre><code>#!/usr/bin/python2
import subprocess
import sys

ip = "10.11.1."

for sub in range (0,255):
    sweep = subprocess.Popen("ping -c 1 "+ ip+str(sub), shell=True, 
    stderr=subprocess.PIPE)
    out = sweep.stderr.read(1)
    sys.stdout.write(out)
</code></pre>

<p>How would I add the part dumping relevant information into a .txt file?</p>
