the_chaos_magus the_chaos_magus - 1 year ago 72
Python Question

Reading a pre-formatted file using read() in python

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.

def load_Intro():
liOb = open('loadgame.txt','r')
while True:
ch =
if ch == "\n": continue
elif ch is None: break


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 :

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.

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

Date : October 21, 1997

{More Content}

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.

Answer Source

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

import sys
import time

liOb = open('loadgame.txt','r')

for i in range(len(content)):
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download