Reading a pre-formatted file using read() in python
<p>I am trying to build a mini text based game just as a first project in python. I decided to write my story and content in text files. I am designing the game such that each character from the file is read and immediately printed out to the screen. </p>

<pre><code>def load_Intro():
liOb = open('loadgame.txt','r')
while True:
    ch = liOb.read(1)
    sys.stdout.write(ch)
    time.sleep(0.002)
    sys.stdout.flush()
    if ch == "\n": continue
    elif ch is None: break

print("\n")
</code></pre>

<p>However my text files have multiple paragraphs separated by one or two lines, and some of its own formatting. For example here is an excerpt from yet to be finalized intr :  </p>

<blockquote>
  <p>You are Max, a college student who lives a very ordinary life. One
  day, you decide to get out of this ordinary life and do something
  worth adventurous and be proud of! You skipped college for a week and
  worked overtime in your workplace, just to get enough money. For the
  past 1 week, you worked 16 hours a day, and had saved enough money for
  a cozy little vacation in the small town of Belleyard Upon Tyne. You
  pack your bags, and leave your house the next day.</p>
  
  <p>Since you had to spend your money wisely, you decide to spend your
  days in a older hotel. You checked in a hotel called The Silver Mare.
  Your first two days were spent awesome. However this morning, after
  you wake up, you wake up feeling tired. Not only that, few times you
  felt someone following and creeping behind you. You looked back
  multiple times...... only to notice there's no one there. You ignored
  that feeling away as hangover due  and prepare to enjoy your remaining
  holidays.</p>
  
  <p>Date  : October 21, 1997</p>
  
  <p>{More Content}</p>
</blockquote>

<p>When the program is run, it reads the first paragraph fine. Correct me if I am wrong, the last condition detects there are no more characters to detect, and is stuck in the loop. I need help in figuring out how to read and display the contents of the file exactly in the same format my file is written in, character by character till the end of the file.</p>

<p>Why try to read character by character? Read it all into the buffer at once, and then display it character by character</p>

<pre><code>import sys
import time

liOb = open('loadgame.txt','r')
content=str(liOb.read())

for i in range(len(content)):
    sys.stdout.write(content[i])
    time.sleep(0.002)
    sys.stdout.flush()
</code></pre>

python python-3.x python-3.5