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.
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
Asked: 2023-06-14 04:42:48 +0000
Seen: 18 times
Last updated: Jun 14 '23
I keep receiving a 404 error while running the application on AWS EC2, can you help me with that?
How do I resolve a 502 error when attempting to call an HTTPS REST API from an HTTP REST API?
In a Bootstrap 5.1 Modal popup, why is the property 'classList' unable to be read for undefined?
How can the issue of an image not being shown in ASP.NET MVC be resolved?