[lug] Shell commands in interactive mode
cary miller
cmiller at math.cudenver.edu
Wed Jul 25 13:06:30 MDT 2001
Hi
In tcl interactive mode you have access to shell commands like pwd and
ls. I wanted python to do that and came up with the following. This
code goes in pythonstartup. The nice thing about it is that you just
type
>>> ls
or
>>> pwd
and get exactly what you would see if you typed the same in the shell.
Problems:
1. Blank line after the output. The problem here is __repr__ is
required to return a string. Is there any way around this?
2. Only works for one-word commands. Cannot pass options to ls,
and cannot do this
>>> cat foo.py
Is there a way to do multiword shell commands??
import os
class ShellCommand:
def __init__(self,cmd):
self.cmd=cmd
def __repr__(self):
eval('os.system(self.cmd)')
return ''
pwd = ShellCommand('pwd')
ls = ShellCommand('ls')
Here is the best I aan come up with for cat.
def cat(file):
for line in open(file).readlines():
print line,
--
Cary Miller
Math Deparment
University of Colorado - Denver
cmiller at math.cudenver.edu
http://www-math.cudenver.edu/~cmiller
More information about the LUG
mailing list