简体   繁体   中英

How to get the output from an external program calling in Python?

I have installed the Vienna package for Windows from the following link:

https://www.tbi.univie.ac.at/RNA/#

and I found that these package cannot be installed over conda in Windows, but I found a turnaround way that it was to call directly the exe file. The information I found it in this post:

https://www.biostars.org/p/394622/

So, I have tested by the command prompt and I got the following answer from a couple of sequences:

在此处输入图像描述

the program that I made in Python is the following:

from subprocess import Popen, PIPE
import subprocess
p = subprocess.Popen('RNAcofold.exe', stdin=PIPE,stdout=PIPE,shell=True)
ans = p.communicate('ATGTTGG&CCGTGT'.encode())
print (ans)

I would like to obtain the string that it says "minimum free energy=some value", but instead I got the following output:

(b'', None)

How can I obtain this output?

I'm not sure if this will work with your program but may be able to use subprocess like below:

>>> output = subprocess.run(["RNAcofold.exe", "ATGTTGG&CCGTGT"], capture_output=True, shell=True).stdout
>>> output
b'this is\nthe content\nof test.txt\n'
>>>

You can then split the string and capture only the line you want:

>>> output.decode().split('\n')
['this is', 'the content', 'of test.txt', '']

I have used subprocess few times, but if I understand correctly from the documentation the communicate and stdin=PIPE,stdout=PIPE are handled automatically by run .

If you're interested in keeping the stderr and return code, just expand the first part as below:

>>> all_output = subprocess.run(["RNAcofold.exe", "ATGTTGG&CCGTGT"], capture_output=True, shell=True)
>>> all_output.stdout
b'this is\nthe content\nof test.txt\n'
>>> all_output.stderr
b''

And so on...

After tweaking around, thanks to @Bernardo Trindade one solution was to play with the env variable like this:

p = subprocess.Popen('RNAcofold.exe', stdin=PIPE,stdout=PIPE,shell=True,env={'PATH': 'full_path'})
ans = p.communicate('ATGTTGG&CCGTGT'.encode())
print (ans)

still there is some data missing from the output, but I will still try other options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM