pythonvirtual-file

Handle file input/output of external binary from python


From my Python script I need to call consequently two external binaries in order to process file in two steps:

import os, subprocess
sbp = subprocess.Popen(['program_1', '-i', 'input.file', '-o', 'temp.file'])
sbp = subprocess.Popen(['program_2', '-i', 'temp.file', '-o', 'output.file'])                      
os.remove('temp.file')

However, it would be nice to speed-up the pipe and reduce disk usage by using virtual RAM-based files instead of 'physical' disk-based. I know that I can use StringIO or tempfile.SpooledTemporaryFile() to handle virtual files within Python script, but is there a possibility to pass the link to such file to an external binary?


Solution

  • from subprocess import Popen
    from tempfile import NamedTemporaryFile
    
    tmp = NamedTemporaryFile('w+')
    sbp = Popen(['program_1', '-i', 'input.file', '-o', tmp.name])
    sbp = Popen(['program_2', '-i', tmp.name, '-o', 'output.file'])                      
    tmp.close()
    

    At the end tmp will be delete.