1

I need to run a linux command line program from python. The input to the command line is a path and file name of a data file. There are literally thousands of these commands that need to be run and I first need to create the data file from a process in python. The output of the command line program needs to be read by python as well. It would be so much easier (and faster considering file reading and writing time) to replace the file name with the actual content somehow, and read the output which is to an output file directly into a python string without requiring disk file access. Is this possible, and how can this be implemented?

Imagine:

$ command < infilename > outfilename 

or

$ command --infile filename --outfile filename

replaced by

$ command < {infile content} > {outfile content}

on the command line or something similar in python.

Jacques MALAPRADE
  • 963
  • 4
  • 13
  • 28
  • Have you a file containing file names ? Could you give an example ? – Serge Ballesta Dec 30 '14 at 10:48
  • Take a look at subprocess and sh modules: https://docs.python.org/2/library/subprocess.html and http://amoffat.github.io/sh/ – Maciek Dec 30 '14 at 11:04
  • possible duplicate of [Python - How do I pass a string into subprocess.Popen (using the stdin argument)?](http://stackoverflow.com/questions/163542/python-how-do-i-pass-a-string-into-subprocess-popen-using-the-stdin-argument) – n. m. could be an AI Dec 30 '14 at 11:06
  • Having briefly had a look at the subprocess module and the above question I assume that in my case the following would work: `from subprocess import Popen, PIPE, STDOUT` `input_string = 'bla bla bla' p = Popen(['command', '--infile', '--outfile'], stdout=PIPE, stdin=PIPE, stderr=STDOUT) cmd_stdout = p.communicate(input=input_string)[0] .....` – Jacques MALAPRADE Dec 30 '14 at 12:52
  • I will have a look at this and get back with a working solution. – Jacques MALAPRADE Dec 30 '14 at 13:00

1 Answers1

0

Using the subprocess module in python. The solution was as follows where command and txt_inp can be replaced. txt_inp is the textual content of the infilename above:

import subprocess

txt_inp = 'some text as input'
process = subprocess.Popen('command', stdin=subprocess.PIPE, stdout=\
        subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
t_arr, t_err = process.communicate(input=txt_inp)

t_arr and t_err are the returned stdout and stderr. t_arr can be saved to a file called outfilename which would complete the following example:

command < infilename > outfilename 
Jacques MALAPRADE
  • 963
  • 4
  • 13
  • 28