Xiaobaixiao
V2EX  ›  Python

请教一个关于 Pexpect 库的问题

By Xiaobaixiao at 2017 年 12 月 31 日 · 2720 次点击

#系统环境:

Centos 7 X64
Python 3

##示例 1:

import pexpect

with open('ftp.log', "w") as f:
        child =pexpect.spawn('yum remove ftp', encoding='utf-8',logfile=f,searchwindowsize=20)
        child.expect('\[y\/N\]')
        child.sendline('y')

##示例 2:

import pexpect

def test():
    with open('test.log', "w") as f:
        child =pexpect.spawn('yum remove ftp', encoding='utf-8',logfile=f,searchwindowsize=20)
        child.expect('\[y\/N\]')
        child.sendline('y')
test()

以上两段代码,使用示例 1 可以卸载 ftp,但在示例 2 中却无法卸载 ftp, 而 ftp.log 和 test.log 的输出是相同的,对,完全相同! 代码不同之处仅仅是示例 2 中的代码封装成了函数而已, 实在不明白为什么示例 2 无法按照预期进行操作, 莫非这个问题是库的坑?#手动滑稽

4 条回复  •  2018-01-01 22:59:18 +08:00
cyclelost
   1
cyclelost  
   2018 年 1 月 1 日 via iPhone   ❤️ 1
child.before 看看呗
Xiaobaixiao
   2
Xiaobaixiao  
OP
   2018 年 1 月 1 日
@cyclelost 正文说了输出日志完全相同啊……
lolizeppelin
   3
lolizeppelin  
   2018 年 1 月 1 日 via Android   ❤️ 1
wai 了没有
Xiaobaixiao
   4
Xiaobaixiao  
OP
   2018 年 1 月 1 日
@lolizeppelin 歪了没有?
这里不用 wait 吧
推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
© 2026 V2EX · 30ms · 3.9.8.5