Ask Your Question
2

What is the process for printing 10 lines and having a 30 second delay, continuously looping until the end of file (EOF) is reached?

asked 2023-06-14 04:42:48 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-06-14 04:46:02 +0000

huitzilopochtli gravatar image

Here is a sample code in Python for printing 10 lines with a 30 second delay, continuously looping until the end of file (EOF) is reached:

import time

with open("file.txt") as file:
    while True:
        for i in range(10):
            line = file.readline().strip()
            if not line:
                file.seek(0)
                line = file.readline().strip()
            print(line)
        time.sleep(30)

In this code, we first open a text file named "file.txt" using a with statement which ensures that the file is closed properly after the loop is finished.

We then create an infinite loop using while True. Inside the loop, we use a for loop to print 10 lines from the file. We use the readline() method to read one line at a time and the strip() method to remove any trailing whitespace.

If the end of file (EOF) is reached, we reset the file pointer to the beginning of the file using the seek() method and then read the first line of the file.

After printing 10 lines, we pause the program for 30 seconds using the time.sleep() method.

The program will continue to loop and print 10 lines with a 30 second delay until the end of file is reached.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-14 04:42:48 +0000

Seen: 18 times

Last updated: Jun 14 '23