0

I have an executable I need to run via a python script and the executable takes a path to a file as an argument, like so:

./myExecutable /pathToFileToProcess

I'm hoping I can run my executable with Popen and pass a string representing my "/pathToFileToProcess" data without having to write it to disk.

I believe what I'm trying to do is very similar to the question asked here: Python string as file argument to subprocess

However the solution suggested isn't working for me. Here is how I've tried to implement it:

from subprocess import Popen, STDOUT, PIPE

stringDataFromFile = "the data to be processed"

p = Popen(['./myExecutable'], shell = False, stdout = PIPE, stderr = PIPE, stdin = PIPE)
stdout, stderr = p.communicate(input = stringDataFromFile)
print(stdout)
print(stderr)

When I run this I get the generic usage message for ./myExecutable in stdout which is what happens if you don't supply a file to process. Note: I need to capture the stdout so I have implemented it slightly differently than the suggested.

I'm thinking the difference must be that ./myExecutable won't accept it's input via stdin, where as the program in the linked example will. Or, I'm just doing it wrong.

Any thoughts on how to do this, or if it is even possible?

Community
  • 1
  • 1
Axl
  • 427
  • 2
  • 7
  • 19

1 Answers1

0

I suspect your suggestion about the executable not accepting input via stdin is correct. You can test this by piping the contents of an example input file into your executable, e.g:

cat path_to_file | my_executable
pxul
  • 497
  • 1
  • 3
  • 13
  • Yeah, if I do that I get the usage message as if no file was specified. Bummer, seems like there might not be a way to do this without writing my data string to a file. – Axl Mar 01 '14 at 02:22