pexpect在函数中生成

时间:2012-11-20 13:35:12

标签: python pexpect

当我把它放在一个函数中时,

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
$ 

1 个答案:

答案 0 :(得分: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()