0

I'm writing an application which requires using a 3rd party shell script.

This shell script some-script processes an input file --ifile and saves the results in an output file --ofile.

So currently I can write/read to a tmp file and delete them afterwards

import os

ifile_name = '/tmp/ifile.txt'
ofile_name = '/tmp/ofile.txt'

with open(ifile_name, 'w') as f:
    # write data to ifile for 'some-script'


# format command
command = 'some-script --ifile {ifile} --ofile {ofile}'.format(
    ifile=ifile_name,
    ofile=ofile_name
)

os.system(command)

with open(ofile_name, 'r') as f:
    # read in data from ofile

# clean up and delete tmp files

Is there a way I can directly pass python data to --ifile (e.g. by '\n'.join(arr)) and have the --ofile stream result as back in script without reading to / writing from files? and just string parse?

Dominique
  • 16,450
  • 15
  • 56
  • 112
SumNeuron
  • 4,850
  • 5
  • 39
  • 107

1 Answers1

0

The script will return its output to stdout and the py script will read it via PIPE

with Popen(["your_script_here"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
balderman
  • 22,927
  • 7
  • 34
  • 52
  • Doesn't quite work for me... basically if I take what you have and break the command up to `['bashCommand', '--ifile', ifile, '--ofile', ofile]` then `print(proc.stdout.read())` is `b''` – SumNeuron Mar 29 '19 at 09:21