read input for cmd.Cmd from file

Achim Domma (Procoders) domma at procoders.net
Fri Jun 3 02:59:21 EDT 2005


Hi,

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:

class MyShell(cmd.Cmd):
     def __init__(self,stdin):
         cmd.Cmd.__init__(self,stdin=stdin)
         ...
     ...

if __name__=='__main__':
     if len(sys.argv)==2:
         shell=MyShell(file(sys.argv[1]))
     else:
         shell=MyShell(sys.stdin)
     shell.cmdloop()

Calling 'myshell.py inputfile' with an invalid inputfile, I get an 
error, so it seems that the file is opened. But the shell starts as 
usuall, ignoring the content of the file. There is no output and no 
errors (if I write nonsens into the inputfile).

Could anybody help?

regards,
Achim



More information about the Python-list mailing list