Wrapping an interactive command line application in a Python script

后端 未结 2 836
清酒与你
清酒与你 2020-12-03 21:15

I am interested in controlling an interactive CLI application from Python calls.

I guess at the most basic level I need a Python script that will start a CLI applicat

相关标签:
2条回答
  • 2020-12-03 21:32

    Does PExpect fits your needs?

    0 讨论(0)
  • 2020-12-03 21:43

    Maybe you want something from Subprocess (MOTW).

    I use code like this to make calls out to the shell:

    from subprocess import Popen, PIPE
    
    ## shell out, prompt
    def shell(args, input=''):
        ''' uses subprocess pipes to call out to the shell.
    
        args:  args to the command
        input:  stdin
    
        returns stdout, stderr
        '''
        p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
        stdout, stderr = p.communicate(input=input)
        return stdout, stderr
    
    0 讨论(0)
提交回复
热议问题