473,226 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

read input for cmd.Cmd from file

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
Jul 19 '05 #1
2 2380
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:

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).


[While I'm at it, duplicated from de.comp.lang.python]

Interesting idea. The simplest approach I found was to feed the file
directly into the cmdqueue-Attribute:

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

if __name__ == "__main__":
****import*optparse
****parser*=*optparse.OptionParser()
****parser.add_option("-i",*"--interactive",*action="store_true")
****options,*args*=*parser.parse_args()
****
****c*=*Cmd()
****try:
********filename,*=*args
****except*ValueError:
********pass
****else:
********c.cmdqueue.extend(file(filename))
********if*not*options.interactive:
************c.cmdqueue.append("quit\n")
****
****c*=*c.cmdloop()

$ cat batch.txt
this
that
that
oops
that

$ python2.4 batch_cmd.py batch.txt
this>
*****<that
*****<that
*** Unknown syntax: oops
*****<that
That's all, folks

If you want to continue the session in the interaktive mode:

$ python2.4 batch_cmd.py batch.txt -i
this>
*****<that
*****<that
*** Unknown syntax: oops
*****<that
(Cmd)
Peter

Jul 19 '05 #2
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

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gunnar | last post by:
Hello, I've just written a CPP program that reads integers from a binary file, and used this code while (my_ifstram.read( (char* ) &number, sizeof(int)) { // do something with number } My...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
3
by: Bill Cohagan | last post by:
I'm writing a console app in c# and am encountering a strange problem. I'm trying to use redirection of the standard input stream to read input from a (xml) file. The following code snippet is from...
10
by: Tibby | last post by:
I need to read/write not only text files, but binary as well. It seems like on binary files, it doesn't right the last 10% of the file. -- Thanks --- Outgoing mail is certified Virus...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
1
by: Jose Reckoner | last post by:
I'm running python 2.3 on Windows XP. Anyone have a quick small script to convert .DT1 and .DEM data to ASCII or some other format? I don't need a viewer. Thanks!
8
by: kepioo | last post by:
I currently have an xml input file containing lots of data. My objectiv is to write a script that reports in another xml file only the data I am interested in. Doing this is really easy using SAX....
6
by: portCo | last post by:
Hello there, I am creating a vb application which is some like like a questionare. Application read a text file which contains many questions and display one question and the input is needed...
9
by: vineeth | last post by:
Hello all, I have come across a weird problem, I need to determine the amount of bytes read from a file, but couldn't figure it out , My program does this : __ file = open("somefile") data =...
63
by: Bill Cunningham | last post by:
I don't think I can do this without some help or hints. Here is the code I have. #include <stdio.h> #include <stdlib.h> double input(double input) { int count=0,div=0; double...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.