How to get the output from .jar execution in python codes?

前端 未结 3 360
一个人的身影
一个人的身影 2020-12-29 12:46

I\'m programming the python module that executes SQL to DBMS and retrieves data. I\'m trying to use jdbc jar files instead of native DB drivers. I\'m wondering how to execut

3条回答
  •  时光说笑
    2020-12-29 13:15

    You can read the output through pipe:

    >>> from subprocess import Popen, PIPE, STDOUT
    >>> p = Popen(['java', '-jar', './GET_DB_DATA.jar'], stdout=PIPE, stderr=STDOUT)
    >>> for line in p.stdout:
        print line
    

    As regards passing string to stdin, you can achieve it this way:

    >>> p = Popen(['cat'], stdin=PIPE, stdout=PIPE, stderr=STDOUT)
    >>> stdout, stderr = p.communicate(input='passed_string')
    >>> print stdout
    passed_string
    

提交回复
热议问题