python可以编写脚本_如何编写一个可以获取和执行python命令的python脚本?

我对Python很陌生。我正在尝试修改一个脚本,使其在无限循环中运行,从控制台获取Python代码行并执行Python代码行。在

我说的是可以做以下例子的事情:Shell> myconsole.py

> PredefindFunction ("Hello")

This is the result of the PredefinedFunction: Hello!!!

> a=1

> if a==1:

> print "a=1"

a=1

> quit

Shell>

我试过使用exec()函数。它可以很好地运行我在脚本中定义的函数,但由于某些原因,它不能真正执行所有代码。我不明白它的逻辑。我得到:

^{pr2}$

谁能帮忙吗?在

谢谢,

古尔

嗨,凯尔

代码如下:class cParseTermCmd:

def __init__(self, line = ""):

self.TermPrompt = "t>"

self.oTermPrompt = re.compile("t>", re.IGNORECASE)

self.TermCmdLine = ""

self.line = line

# Check if the TermPrompt (t>) exist in line

def mIsTermCmd (self):

return self.oTermPrompt.match(self.line)

# Remove the term prompt from the terminal command line

def mParseTermCmd (self):

self.TermCmdLine = re.sub(r'%s'%self.TermPrompt, '', self.line, flags=re.IGNORECASE)

exec (self.TermCmdLine)

And I call it in an infinite while loop from:

def GetCmd (self):

line = raw_input('>')

self.TermCmdLine = cParseTermCmd(line)

if self.TermCmdLine.mIsTermCmd():

# Execute terminal command

self.TermCmdLine.mParseTermCmd()

else:

return line

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值