How to make python script press 'enter' when prompted on Shell

前端 未结 1 532
迷失自我
迷失自我 2020-12-30 12:42

I want to automatize upgrade of a program.

I run in Python this code:

import subprocess
subprocess.call(\'./upgrade\')

When I do th

相关标签:
1条回答
  • 2020-12-30 13:22

    You can use subprocess.Popen and subprocess.communicate to send input to another program.

    For example, to send "enter" key to the following program test_enter.py:

    print "press enter..."
    raw_input()
    print "yay!"
    

    You can do the following:

    from subprocess import Popen, PIPE
    p = Popen(['python test_enter.py'], stdin=PIPE, shell=True)
    p.communicate(input='\n')
    

    You may find answer to "how do i write to a python subprocess' stdin" helpful as well.

    0 讨论(0)
提交回复
热议问题