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

前端 未结 3 380
一个人的身影
一个人的身影 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:09

    You could do :

    with open('output_of_jar.txt','w') as fp :
        subprocess.Popen('java -jar ./GET_DB_DATA.jar',stdout=fp).wait()
    with open('output_of_jar.txt') as f :
        output = f.read()
    print output
    

    Edit :

    stdout=fp means that the output of the command will be written to the file output_of_jar.txt

    Then you just have to read the contents of the file with :

    with open('output_of_jar.txt') as f :
        output = f.read()
    print output
    

提交回复
热议问题