How to write a simple shell loop in python?

Scott David Daniels Scott.Daniels at Acm.Org
Wed Jan 21 17:38:28 CET 2009


Dietrich Bollmann wrote:
> I am trying to write a simple shell loop in Python.
> 
> My simple approach works fine - but the first output line after entering
> something is always indented by one blank.  
> Is there any logic explanation for this?  
Yes
> How can I get rid of the blank?
By not asking for it with the comma.
> 
>>>> while (1):
> ...     print "$ ",
> ...     input = sys.stdin.readline()
> ...     input = input.strip()
> ...     print input
> ...     print input
> ...     print input
> ... 
> $ one
The one and \n above are from stdin, not stdout.  So stdout thinks
it is still on the same line as the $.
>  one
The space separates the 'one' from the '$ ' that it output to stdout
above.
> one
> one

--Scott David Daniels
Scott.Daniels at Acm.Org





More information about the Python-list mailing list