简体   繁体   English

从Expect执行Python脚本

[英]Execute a Python script from Expect

I am using Expect for automation, and I want to execute a Python script from it. 我正在使用Expect进行自动化,并且我想从中执行Python脚本。 But it is not working... This is what I have tried so far: 但这不起作用...这是我到目前为止尝试过的:

#!/usr/bin/expect
spawn  "./os_fun"

and

#!/usr/bin/expect
spawn  "./os_fun.py"

and

#!/usr/bin/expect
spawn  python "./os_fun(.py)"

The "os_fun.py" contains the simple code: “ os_fun.py”包含简单的代码:

#!/bin/usr/python
import os
print os.getcwd()

I would also like to mention that I must use Expect only and not Bash as I need to do the automation part, and I am not supposed to use Pexpect. 我还要提及的是,我必须只使用Expect而不能使用Bash,因为我需要执行自动化部分,而且我不应该使用Pexpect。

When it comes to Expect , you always have to expect something, so that Expect will wait for it. 当谈到Expect ,您总是必须期待一些事情,以便Expect将等待它。 Else, it will proceed as such. 否则,它将照此进行。 Simply spawning a processing does not make sense, as Expect don't wait to see for it which in turn makes the user not to see the output as well. 仅仅产生一个处理是没有意义的,因为Expect不要等着看它,这反过来又使用户也看不到输出。

In your case, you just have to run the code and see the output till the program completes. 就您而言,您只需要运行代码并查看输出,直到程序完成即可。 I hope my understanding is correct. 我希望我的理解是正确的。

!/usr/bin/expect
spawn python os_fun.py
expect eof; # will wait till 'eof' seen

Here, expect command will wait till it sees close of the running program. 在这里, expect命令将等到看到正在运行的程序关闭。 Default timeout is 10 seconds which can be changed as 默认超时为10秒,可以更改为

set timeout 60; # Timeout value as 1 min

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM