V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
Xiaobaixiao
V2EX  ›  Python

请教一个关于 Pexpect 库的问题

  •  
  •   Xiaobaixiao · Dec 31, 2017 · 2391 views
    This topic created in 3047 days ago, the information mentioned may be changed or developed.

    #系统环境:

    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 replies    2018-01-01 22:59:18 +08:00
    cyclelost
        1
    cyclelost  
       Jan 1, 2018 via iPhone   ❤️ 1
    child.before 看看呗
    Xiaobaixiao
        2
    Xiaobaixiao  
    OP
       Jan 1, 2018
    @cyclelost 正文说了输出日志完全相同啊……
    lolizeppelin
        3
    lolizeppelin  
       Jan 1, 2018 via Android   ❤️ 1
    wai 了没有
    Xiaobaixiao
        4
    Xiaobaixiao  
    OP
       Jan 1, 2018
    @lolizeppelin 歪了没有?
    这里不用 wait 吧
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5698 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 08:43 · PVG 16:43 · LAX 01:43 · JFK 04:43
    ♥ Do have faith in what you're doing.