How to use readline interactively?

John Farrell jfarrell at mincom.com
Tue Oct 5 21:46:30 EDT 1999


Hi everybody! I have some code like this which implements a simple command
loop:

import sys
while 1:
    print "> ",
    s = sys.stdin.readline()
    if s[-1] == '\n': s = s[:-1]
    if len(s) == 0: break
    print "Hello " + s    

What's wrong with it is that there is always a spare space at the beginning
of the first line of output after the prompt:

> Fred
 Hello Fred
>

I want to get rid of that space before Hello. This must be a common
problem, but it doesn't seem to be in the FAQ. Thanks for any advice.
BTW, I can't remember why I am not using raw_input(), there was a good
reason at the time. Thanks,

John




More information about the Python-list mailing list