program in interactive mode

Mike Meyer mwm at
Mon Dec 27 05:49:39 CET 2004

"B.G.R." <nospam at> writes:

> Hi all,
> I'm working on an interpreter for a university subject, which is programmed
> in python under linux.
> I got most of the work done and I'm just trying to settle some problems I've
> found on my way.
> Right now, the most important is reading the user input. Everything goes
> through the standard input, and now I got something like this:
> numline=0
> for line in sys.stdin:
>     numline+=1
>     workwithline(line)
> A little bit more complex, but that's the idea. That will work if the user
> does something like "./ < code" or "cat code | ./", and
> that's ok, but if the user only does "./" 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
> not.
> I used to think that the piped program doesn't know anything and it's just
> OS dependant to close and open the right descriptors, but I'm not sure
> anymore. Any help or pointer in the right direction would be greatly
> appreciated.

I've discovered a truly elegant trick with python programs that
interpret other data. You make them ignore lines that start with # at
the beginning of the line, and accept the name of a file to be
interpreted as a first argument. Your users can then put


at the top of their files, and then treat their files as normal
executables. has to be on their path; if not, they need to
plug in the full path to

I save the state of TkInter programs by writing this out then pickling
the objects that define the state to the file. Executing that file
will bring the program back up in the same state it was saved in.

Mike Meyer <mwm at>
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

More information about the Python-list mailing list