read input for cmd.Cmd from file

Peter Otten __peter__ at web.de
Fri Jun 3 04:32:54 EDT 2005


Achim Domma (Procoders) wrote:

> I'm writing a simple shell using cmd.Cmd. It would be very usefull if I
> could read the commands as batchjob from a file. I've tried the following:

[...]

Your original approach should work too if you clear the use_rawinput flag
and provide a do_EOF() method that handles the file end:

import cmd

class Cmd(cmd.Cmd):
    def do_this(self, arg):
        print "this>", arg
    def do_that(self, arg):
        print "     <that", arg
    def do_quit(self, arg):
        print "That's all, folks"
        return True
    do_EOF = do_quit

if __name__ == "__main__":
    import sys
    filename = sys.argv[1]
    c = Cmd(stdin=file(filename))
    c.use_rawinput = False
    c = c.cmdloop()

Peter




More information about the Python-list mailing list