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>The problem is that, after <code>readline()</code> reaches the end of the file, it will continue to return empty lines.  You need something that ends the loop so that you can start over on the beginning of the file:</p>

<pre><code>from time import sleep

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

<p>If you really want to use <code>readline</code>, then you need to test for the end of file.  While <code>readline</code> is reading actual lines, it will always return at least a newline character.  If it returns nothing, then it has reached the end of the file.  Thus:</p>

<pre><code>from time import sleep

while True:
    with open ("sample.txt", 'r') as test:
        while True:
            line = test.readline()
            if not line:
                break
            print line.rstrip()
            sleep(3)
</code></pre>

python python-2.7