Python Forum
Need better solution for time-delayed printing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need better solution for time-delayed printing
#1
You have my sincerest apologies if my question is terribly basic. I am very new to Python, but I have tried to exhaust all of my resources for the last couple of days before posting on this forum.

My goal is to create a looped function that scrolls the contents of a large .txt file vertically on a screen (with a time delay to make the text readable). My code so far is:

import time

f = open('Review.txt', 'r')
file_contents = f.read()
x = 0
while x <41000:
	print (file_contents[x], end='')
	x += 1
	time.sleep(.01)
f.close()
This is broadly functional, but there are two issues that I cannot find a solution to:

  1. This method only prints full paragraphs at a time. I would prefer to print one character at a time.
  2. This method cuts words in half over multiple lines, puts commas alone at the beginning of lines, etc.
Is anyone willing to point me in the right direction to better implement this idea?
Reply
#2
Why are you doing this? If you want something useful then I think you are going about it all wrong. If this is a homework assignment then what are the requirements/limitations?
Reply
#3
(Feb-04-2021, 07:17 PM)deanhystad Wrote: Why are you doing this? If you want something useful then I think you are going about it all wrong. If this is a homework assignment then what are the requirements/limitations?

That's fair, I may be way off in my solution.

My partner is obsessed with the Lord of the Rings. I wanted to explore the feasibility of creating a display that would constantly scroll the text of the trilogy, probably run off of a Raspberry Pi.
Reply
#4
That is a fun project. What is your display?
Reply
#5
(Feb-04-2021, 07:27 PM)deanhystad Wrote: That is a fun project. What is your display?

Thanks! I'll be honest, I haven't purchased a display yet. I was waiting to see if the project was feasible and if I could figure out the code. The screen I was considering was this Waveshare display.
Reply
#6
Quote:This method only prints full paragraphs at a time. I would prefer to print one character at a time.

Sort of. The python code is definitely printing out one character at a time. But the screen buffers it, and only shows it to you once it sees a new line. You can flush it to force it to update.

You also don't need to index the string directly, you can iterate over individual characters.
import sys
import time

contents = ''
with open('the_file.txt') as f:
    contents = f.read()

for ch in contents:
    print(ch, end='')
    sys.stdout.flush()
    time.sleep(0.01)
print()
bowlofred likes this post
Reply
#7
You can flush directly in the print as well.

for ch in contents:
    print(ch, end="", flush=True)
    time.sleep(0.01)
nilamo likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyad time conversion error (plus solution) PapaZod 2 3,491 Nov-25-2018, 02:42 PM
Last Post: PapaZod

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020