How to print a file line by line in specific interval continuously using python?
<p>I have a file with 4 website names as shown below. I want to print each website name one by one continuously, in specific interval.</p>

<p>sample.txt:</p>

<pre><code>facebook.com
gmail.com
test.com
yahoo.com
</code></pre>

<p>I have tried with following code. But its print website names only once. I wanted to website names continuously.</p>

<pre><code>from time import sleep

while True:
    with open ("sample.txt", 'r') as test:
        while True:
            print test.readline()
            sleep(3)
    pass
</code></pre>

<p>Expected output:</p>

<pre><code>facebook.com
gmail.com
test.com
yahoo.com
facebook.com
gmail.com
test.com
yahoo.com
facebook.com
gmail.com
test.com
yahoo.com
facebook.com
gmail.com
test.com
yahoo.com
.
.
.   
</code></pre>

<p>Can I get help to fix this issue?</p>

<p>Thanks.     </p>

<p>Your file object which returns an <em>iterator</em> got exhausted after the first round of calls to <code>readline()</code>. You should instead read the entire file into a list and iterate over that list successively. </p>

<pre><code>from time import sleep


with open ("sample.txt") as test:
    lines = test.readlines() # read all lines into list

while True:
    for line in lines:
        print line
        sleep(3)
</code></pre>

python python-2.7