2

当我将 pexpect.spawn 放入函数时,它似乎失败了。在下面的示例中,我希望它同时触及“/tmp/no.txt”和“/tmp/yes.txt”。它只涉及/tmp/yes.txt。我已经尝试了“import pexpect”的所有明显位置。它不会抛出错误,只是不会运行。感谢您的任何建议!

import pexpect
def fun():
    import pexpect
    fail = pexpect.spawn('touch /tmp/no.txt')

fun()
succeed = pexpect.spawn('touch /tmp/yes.txt')

$ ls /tmp/*.txt
/tmp/yes.txt
$ 
4

1 回答 1

3

您需要等待命令运行:

import pexpect
def fun():
    #import pexpect you've already imported pexpect you don't need to import it again 
    fail = pexpect.spawn('touch /tmp/no.txt')
    fail.wait()

fun()
succeed = pexpect.spawn('touch /tmp/yes.txt')
succeed.wait()
于 2012-11-20T13:45:49.933 回答