无法通过Pexpect从其他目录启动程序

时间:2015-06-02 13:28:55

标签: python bash command-line-interface pexpect

我很难通过Pexpect模块启动一个简单的HelloWorld程序。 我有一个带有HelloWorld二进制文件的目录 - hw,expect script - m.py,以及一个内部具有相同脚本的目录。

├── hw
├── m.py
├── main.cpp
└── dir
    └── m.py

这是我的期望脚本:

import pexpect
child = pexpect.spawn("./hw", cwd = /absolute/path/to/parent/directory")
child.expect("!")
print(child.before)

如果我从父目录运行脚本,一切都很好。但是,如果我从任何其他目录运行它,如dir here,我会收到以下错误:

pexpect.ExceptionPexpect: The command was not found or was not executable: ./hw.

我该如何对抗?

我在Mac OS和Ubuntu上试过这个。 HelloWorld二进制文件工作正常,它被设置为可执行文件。 Python 2.7.6,pexpect 3.3

1 个答案:

答案 0 :(得分:2)

要运行可执行文件.AutoFilter,其父目录应位于hw envvar中,或者您应提供完整路径。如果路径是相对的(不推荐),则它是相对于当前工作目录的路径,而不管PATH值。

如果您想从其目录中运行cwd

hw
相关问题