pinging websites from array in a loop
<pre><code>import subproccess
import sys

mylist= ['google.com','bbc.com','yahoo.com','gmail.com','hotmail.com',
         'amazon.com']

for ping in mylist(0,5):
    result = os.system("ping  %s" % ping)
    result.stdout=open("test.txt","w")
    result.stdout.close()
</code></pre>

<p>Can someone please find the mistake in my code? I want to call this script from cmd terminal. The aim of this code is to ping each of the website every time and then write the result in a text file. I'm quite new with python and I don't know how to build a proper code.</p>

<ul>
<li>os not imported</li>
<li>mylist = ['google.com','bbc.com','yahoo.com','gmail.com','hotmail.com','amazon.com']</li>
<li>for ping in mylist:</li>
<li>result = os.system("ping  -c 1 %s" % ping)</li>
<li><p>f = open("test.txt","w") (open the file before entering the loop, else it would rewrite the file everytime the loop iterates)</p>

<pre><code>import os

mylist= ['google.com','bbc.com','yahoo.com','gmail.com','hotmail.com',
         'amazon.com']

f = open("file.txt", 'w')
for ping in mylist:
    result = os.system("ping -c 1 %s" % ping)
    f.write(ping + " : " +str(result) + "\n")
f.close()
</code></pre></li>
</ul>

python arrays loops cmd ping