program in interactive mode

Alex Martelli aleaxit at yahoo.com
Sun Dec 26 15:32:46 EST 2004


B.G.R. <nospam at yahoo.tk> wrote:
   ...
> numline=0
> for line in sys.stdin:
>     numline+=1
>     workwithline(line)

Consider the alternative:
    for numline, line in enumerate(sys.stdin):

Nothing to do with your main program, but still neater...

> that's ok, but if the user only does "./myprog.py" then I got to get into
> interactive mode and show a prompt in every line expecting the user input
> for that line. Problem is I don't know how to tell if I've been "piped" or

sys.stdin.isatty() should serve you well.


Alex



More information about the Python-list mailing list