简体   繁体   中英

How to make a subprocess object save output to a temporary file and then obtain values from temporary file?

I am currently using a Unix-based executable within my Python code in order to analyze some DNA sequences. After running the analysis, this particular executable the writes the output to a file, which is then read by my code and has the relevant values acquired from it.

Since the file is not needed for anything apart from yielding some values, I decided to make it a temporary file that would be deleted after the necessary information was acquired from it. However, my efforts to set up the code to accomplish this has proved fruitless.

import subprocess as sb
import tempfile

def calculate_complex_mfe(DNA_seq):
    complex_seq= str(DNA_seq)
    with tempfile.NamedTemporaryFile as mfefile:
        p= sb.Popen(['/Users/john/Documents/Biology/nupack3.0.6/bin/mfe', '-T', '41'], stdin=sb.PIPE, stdout=sb.PIPE, env=my_env)
        strb = (mfefile+'\n'+ complex_seq + '\n').encode('utf-8')
        data = p.communicate(input=strb)
        mfe= float(open(mfefile+'.mfe').readlines()[14])
    return mfe

I should note that the strb variable is set up to match the formatting required for the executable to run. Also for what it's worth, the original code I used that functioned properly is posted below.

def calculate_complex_mfe(DNA_seq):
    complex_seq= str(DNA_seq)
    filename= "mfe-file"
    p= sb.Popen(['/Users/john/Documents/Biology/nupack3.0.6/bin/mfe', '-T', '41'], stdin=sb.PIPE, stdout=sb.PIPE, env=my_env)
    strb = (filename+'\n'+ complex_seq + '\n').encode('utf-8')
    data = p.communicate(input=strb)
    mfe= float(open(filename+'.mfe').readlines()[14])
    return mfe

Can anybody tell me how I can create a temporary file for the output of an executable so I can obtain some value(s) from it?

From the documentation:

NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None, newline=None, suffix='', prefix='tmp', dir=None, delete=True) Create and return a temporary file.

Returns an object with a file-like interface; the name of the file is accessible as file.name. The file will be automatically deleted when it is closed unless the 'delete' argument is set to False.

You don't need a file handle to write to, you just need to create a temporary file name , not open a temporary file. Just replace:

filename= "mfe-file"

by

filename = tempfile.mktemp()

in your second snippet to generate a temporary file name (full path) for you

Don't forget to os.remove(filename) it when you're done with it.

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